Excel VBA - How to modify my code to include a command that prints data to PDF

VanGirl

New Member
Joined
Apr 16, 2024
Messages
3
Office Version
  1. 365
Platform
  1. MacOS
Please help to modify this code to include the following command, if column H:H in ws.Neonpay is equal to "B" print to PDF a list of the lines in column A:A in wsNeonpay that correspond to the cells in column H:H that are equal to "B" for each of the merchants in the merchant list and if column H:H is equal to "A" then skip. The PDF files printed for each merchant should be saved in the Settlements folder path.

VBA Code:
Sub Button1_Click()

    Dim ws As Worksheet
    Dim wsNeonpay As Worksheet
    Dim wsCandlestick As Worksheet
    Dim wsData As Worksheet
    Dim pdfName As String
    Dim pdfPath As String
    Dim rng As Range
    Dim merchantCell As Range
    Dim merchantList As Range
    Dim startDate As Date
    Dim endDate As Date
    Dim active As Date
    
    ' Define the Settlements folder path
    Dim settlementsFolderPath As String
    settlementsFolderPath = ThisWorkbook.Path & "/Settlements"
    
    ' Check if the Settlements folder exists, and create it if it doesn't
     If Len(Dir(settlementsFolderPath, vbDirectory)) = 0 Then
        On Error Resume Next
        MkDir settlementsFolderPath
        If Err.Number <> 0 Then
            MsgBox "Error creating folder: " & Err.Description
            ' Handle error or exit
        End If
        On Error GoTo 0
        
     End If

    ' Set references to your worksheets
    Set ws = ThisWorkbook.Sheets("Settlement Template") ' Modify with actual sheet name
    Set wsData = ThisWorkbook.Sheets("Fees") ' Sheet with merchant data
    Set wsNeonpay = ThisWorkbook.Sheets("Neonpay (2)")
    On Error Resume Next
    Set wsCandlestick = ThisWorkbook.Sheets("candlestick")
    On Error GoTo 0
    If wsCandlestick Is Nothing Then
        MsgBox "The sheet 'candlestick' was not found."
        Exit Sub
    End If
    
    ' Clear any existing filters on Neonpay (2)
    If wsNeonpay.AutoFilterMode Then wsNeonpay.AutoFilterMode = False
    wsNeonpay.Range("S1:S" & wsNeonpay.Cells(wsNeonpay.Rows.Count, "S").End(xlUp).Row).AutoFilter Field:=1
    
    ' Apply AutoFilter to the entire Date column, without specific criteria
    Dim dateColumn As Long
    dateColumn = 19 ' Assuming the date is in Column S(19)
    
    Dim neonpayLastRow As Long
    neonpayLastRow = wsNeonpay.Cells(wsNeonpay.Rows.Count, dateColumn).End(xlUp).Row
    wsNeonpay.Range("S1:S" & neonpayLastRow).AutoFilter Field:=1
    
    active = ws.Cells(20, 19).Value
    
    ' Define the print area (modify if necessary)
    Set rng = ws.Range("A1:I33")
    ws.PageSetup.PrintArea = rng.Address

    ' Define the range with the list of merchants
    Set merchantList = wsData.Range("A2:A350") ' Modify to your range
    
    ' Reference the workbook and worksheet for the settlement summary
    Dim wbSummary As Workbook
    Dim wsSummary As Worksheet
    On Error Resume Next
    Set wbSummary = Workbooks("SettlementSummary")
    On Error GoTo 0
    
    Dim wasWorkbookOpened As Boolean
    
    If wbSummary Is Nothing Then
    
        ' Open the workbook
        Set wbSummary = Workbooks.Open(FileName:=ThisWorkbook.Path & "/SettlementSummary.xlsx")
        wasWorkbookOpened = True
        
    End If
    
    Set wsSummary = wbSummary.Sheets("Settlement Summary")
    
    SortCandlestickData

     Dim nextRow As Long
     ' Loop through each merchant
     For Each merchantCell In merchantList
    
     wsNeonpay.Range("A2:S" & wsNeonpay.Cells(wsNeonpay.Rows.Count, "S").End(xlUp).Row).AutoFilter Field:=19, Criteria1:="<>"
        
        ' Check if the cell is not empty
        If Not IsEmpty(merchantCell.Value) Then
            ws.Range("B5").Value = merchantCell.Value
            ws.Range("P17").Value = wsData.Cells(merchantCell.Row, 16).Value
            SortCandlestickData
            
            If ws.Range("I30").Value <> 0 Then
                startDate = active - (6 + wsData.Cells(merchantCell.Row, 16).Value)
                endDate = active - wsData.Cells(merchantCell.Row, 16).Value
                            
                
                 ' Apply specific date filters for this merchant

                Dim fullRange As Range
                Dim lastRow As Long
                lastRow = wsNeonpay.Cells(wsNeonpay.Rows.Count, "R").End(xlUp).Row
                Set filterRange = wsNeonpay.Range("A2:S" & lastRow)
                filterRange.AutoFilter Field:=19, Criteria1:=">=" & startDate, Operator:=xlAnd, Criteria2:="<=" & endDate

                Dim cleanedMerchantValue As String
                cleanedMerchantValue = Trim(Application.Clean(merchantCell.Value))
                
                Dim merchantRow As Long
                
                merchantRow = Application.WorksheetFunction.Match(cleanedMerchantValue, wsSummary.Columns("A:A"), 0)

                ' Populate values from 'Settlement Template' to the corresponding row
                wsSummary.Cells(merchantRow, "B").Value = ws.Range("I13").Value ' Populate column B
                wsSummary.Cells(merchantRow, "D").Value = ws.Range("I30").Value ' Populate column D
                
                ' Define PDF name and path (possibly unique for each merchant)
                pdfName = ws.Range("B5").Value & "_SettlementReport_" & ws.Range("B6").Value & "(SettlementDate_" & ws.Range("B7").Value & ")" & ".pdf"
                pdfPath = settlementsFolderPath & "/" & pdfName
    
    
                ' Export as PDF
                ws.ExportAsFixedFormat Type:=xlTypePDF, FileName:=pdfPath, Quality:=xlQualityStandard, _
                    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
                
            
            End If
            wsNeonpay.AutoFilterMode = False
            wsNeonpay.Range("A2:S" & wsNeonpay.Cells(wsNeonpay.Rows.Count, "S").End(xlUp).Row).AutoFilter Field:=19, Criteria1:="<>"
        End If
    Next merchantCell
    
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
To elaborate, the code currently runs through a list of merchants and populates data for each merchant in to a 'settlements tab'. The populated information then get's printed to pdf for each merchant and simultaneously data corresponding to each merchant get's populated into a 'Settlement Summary' excel sheet. All these documents are saved into the Settlements folder path. Now I would like to add an additional command where each merchant with the letter 'B' in column H:H of wsNeonpay has the list of ID's in column A:A of wsNeonpay corresponding to those merchants printed to PDF and saved in the Settlements folder path. Appreciate any help with this.
 
Upvote 0

Forum statistics

Threads
1,223,933
Messages
6,175,479
Members
452,647
Latest member
MatthewBiersay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top