Page 1 of 1

สอบถามการแก้ไข Filter ครับ และ Export PDF แบบมีเงื่อนไข

Posted: Mon Apr 03, 2017 10:59 pm
by primeval

Code: Select all

Sub Button17_Sort2()
Dim stword1 As String
Dim stword2 As String
Dim stword3 As String
Dim stword4 As String
Dim stword5 As String
Dim stword6 As String
stword3 = Mid(Range("A1"), 7, 50)
stword = "รายงานต่อใบอนุญาต สาขา"
stword4 = "นายหน้า ประกันวินาศภัย"
stword5 = "ประเภทใบอนุญาต"
Range("B5") = stword & " " & stword3 & " " & stword5 & " " & stword4
Range("E3").Select
ActiveSheet.ListObjects("Table4").TableStyle = "TableStyleLight18"
    ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=13, Criteria1:= _
        "=นายหน้า", Operator:=xlOr, Criteria2:="=โบรคเกอร์"
                    ActiveWorkbook.Worksheets("Sort").ListObjects("Table4").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sort").ListObjects("Table4").Sort.SortFields.Add _
        Key:=Range("Table4[ครั้งที่]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortTextAsNumbers
    ActiveWorkbook.Worksheets("Sort").ListObjects("Table4").Sort.SortFields.Add _
        Key:=Range("Table4[วันหมดอายุ]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sort").ListObjects("Table4").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
        Range("SortTable").Select
    Selection.Rows.AutoFit
    Columns("B:I").Columns.AutoFit
    Columns("J:K").ColumnWidth = 13.88
    Columns("L:L").EntireColumn.Hidden = True
     Columns("M:M").Columns.AutoFit
     Columns("N:N").ColumnWidth = 11
End Sub
1.อยากให้มีเงื่อนไข ตรงช่องรหัสตัวแทน ถ้าพบข้อความคำว่า "ไม่มีรหัส" ไม่ให้แสดงข้อมูลต้องทำยังไงครับ ผมใช้วิธีการบันทึก Macro ครับ

Code: Select all

Sub Button28_PDF()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim codebranch As String
Dim branchname As String
Dim lictype As String
On Error GoTo errHandler


lictype = Mid(Range("B5"), 48, 7)
codebranch = Left(Range("A1"), 3)
branchname = Mid(Range("A1"), 7, 50)
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "dd_mm_yy")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = lictype & "_" & codebranch & "_" & branchname & "_" & strTime & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
2.เวลา Export PDF กรณีผมกด Macro Sort ทั้งหมด ตอน Export PDF อยากให้แสดง ร่วมใบอนญาต _003_ลุมพินี_03_04_60.pdf

ขอบคุณครับ

Re: สอบถามการแก้ไข Filter ครับ และ Export PDF แบบมีเงื่อนไข

Posted: Tue Apr 04, 2017 9:13 pm
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

'...Other code
Range("E3").Select
ActiveSheet.ListObjects("Table4").TableStyle = "TableStyleLight18"
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=13, Criteria1:= _
    "=นายหน้า", Operator:=xlOr, Criteria2:="=โบรคเกอร์"
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=6, Criteria1:= _
    "<>ไม่มีรหัส"
'...Other code
    
ส่วนข้อ 2 อ่านแล้วไม่เข้าใจว่าต้องการให้แสดงผลที่ชีตไหน เซลล์ไหน หรือแสดงที่ใด ช่วยอธิบายมาอีกรอบครับ

Re: สอบถามการแก้ไข Filter ครับ และ Export PDF แบบมีเงื่อนไข

Posted: Tue Apr 04, 2017 9:58 pm
by primeval
2.ขออธิบายใหม่นะครับ ตรงช่อง
Cell = B5
/
รายงานต่อใบอนุญาต สาขา ประเภทใบอนุญาต นายหน้า ประกันวินาศภัย
อยากให้ค้นหาคำว่า นายหน้า ซึ่งถ้าพบคำว่า "นายหน้า" ให้เก็บค่านี้ไว้
//
รายงานต่อใบอนุญาต สาขา ประเภทใบอนุญาต ตัวแทน ประกันวินาศภัย
อยากให้ค้นหาคำว่า นายหน้า ซึ่งถ้าพบคำว่า "ตัวแทน" ให้เก็บค่านี้ไว้
///
รายงานต่อใบอนุญาต สาขา ลุมพินี ร่วมใบอนุญาตประกันวินาศภัยทุกประเภท
อยากให้ค้นหาคำว่า ร่วมใบอนุญาต ซึ่งถ้าพบคำว่า "ร่วมใบอนุญาต" ให้เก็บค่านี้ไว้
รายงานต่อใบอนุญาต สาขา ประเภทใบอนุญาต นายหน้า ประกันวินาศภัย
รายงานต่อใบอนุญาต สาขา ประเภทใบอนุญาต ตัวแทน ประกันวินาศภัย
รายงานต่อใบอนุญาต สาขา ลุมพินี ร่วมใบอนุญาตประกันวินาศภัยทุกประเภท
ข้อความพวกนี้มาจากการกด Macro ใน Sheet Sort ครับ ตรงหัวด้านบน

Code: Select all

Other
Dim codebranch As String 'ประกาศตัวแปร 
Dim branchname As String
Dim lictype As String
On Error GoTo errHandler


lictype = Mid(Range("B5"), 48, 7) 'ตรงส่วนที่เก็บค่าตัวแปรจาก Cell = B5 ผมไม่เข้าใจคำสั่ง Cell.Find Text ก็เลยใช้ MID ดึงค่าจาก B5 มาตัด
codebranch = Left(Range("A1"), 3)
branchname = Mid(Range("A1"), 7, 50)
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "dd_mm_yy")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = lictype & "_" & codebranch & "_" & branchname & "_" & strTime & ".pdf"
Other
'ผลลัพธ์หลังจากเก็บค่าตัวแปรแสดงผล
strFile = lictype
หรือขึ้นต้นตัวแรกครับ พอกดปุ่ม Macro Export PDF จาก Sheet Sort
ก่อนบันทึกจะมีให้ตั้งชื่อ จะได้รูปแบบ ดังนี้


ถ้าเป็นตัวแทน
ตว._003_ลุมพินี_03_04_60.pdf
ถ้าเป็นนายหน้า
ตว._003_ลุมพินี_03_04_60.pdf
ถ้าเป็นร่วมใบอนุญาต
ร่วมใบอนุญาต_003_ลุมพินี_03_04_60.pdf

Re: สอบถามการแก้ไข Filter ครับ และ Export PDF แบบมีเงื่อนไข

Posted: Tue Apr 04, 2017 11:01 pm
by snasui
:D อ่านแล้วยังไม่กระจ่างเท่าไร เข้าใจว่าต้องการให้ตรวจสอบค่าใน B5 แล้วกำหนดค่าตัวแปรตามค่าต่าง ๆ ที่พบ

ตัวอย่าง Code ครับ

Code: Select all

'...Other code
On Error GoTo errHandler

If InStr(Range("b5").value, "นายหน้า") > 0 Then
    lictype = "นายหน้า"
ElseIf InStr(Range("b5").value, "ตัวแทน") > 0 Then
    lictype = "ตัวแทน"
ElseIf InStr(Range("b5").value, "ร่วมใบอนุญาต") > 0 Then
    lictype = "ร่วมใบอนุญาต"
End If
'lictype = VBA.Mid(Range("B5"), 48, 7)
'...Other code

Re: สอบถามการแก้ไข Filter ครับ และ Export PDF แบบมีเงื่อนไข

Posted: Tue Apr 04, 2017 11:46 pm
by primeval
snasui wrote::D อ่านแล้วยังไม่กระจ่างเท่าไร เข้าใจว่าต้องการให้ตรวจสอบค่าใน B5 แล้วกำหนดค่าตัวแปรตามค่าต่าง ๆ ที่พบ

ตัวอย่าง Code ครับ

Code: Select all

'...Other code
On Error GoTo errHandler

If InStr(Range("b5").value, "นายหน้า") > 0 Then
    lictype = "นายหน้า"
ElseIf InStr(Range("b5").value, "ตัวแทน") > 0 Then
    lictype = "ตัวแทน"
ElseIf InStr(Range("b5").value, "ร่วมใบอนุญาต") > 0 Then
    lictype = "ร่วมใบอนุญาต"
End If
'lictype = VBA.Mid(Range("B5"), 48, 7)
'...Other code
อันนี้แหละครับ ถูกประเด็นเปะๆ ขอบคุณมากครับ
ขออนุญาตสอบถามอีกเรื่องหนึ่ง กรณีผมอยาก ร่วมกลุ่มข้อมูล สาขา เช่น สาขา A1:A10 เรียกว่า กลุ่ม 1 แล้วสั่ง Export PDF ในกลุ่ม 1 โดยตอนสั่งExport กลุ่ม 1 ทั้งหมด ให้ export แยกชื่อแยกไฟล์ทีละสาขา แล้วระบุว่าเงื่อนไข ตัวแทน พรบ. ต้องศึกษาเรื่องอะไรมั้งครับ