AutoFilter to Create PDFs Using 2 Files

tanyamc

New Member
Joined
Aug 24, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I modified this script (Macro to convert filtered list to multiple pdf -) to my situation, where the list of records in on file is used to filter the source data to create a PDF for that record. I have pasted the script below. It is creating the PDFs, but the only data included in them is the header row of the source data.

I attached screenshots of the worklist and the source data related to one of the items on the list to show columns that match up. (I can't use add-ins to post the minisheet.)

Thank you for any helps. TM


Some items (PD related) are commented out because they are for use as this evolves to include more files etc.

Sub PracticeToPDF()
'Prepared by Dr Moxie
'Edited by Tanya


Dim wbk_worklist As Workbook
'same as workbook this macro lives in

Dim ws_unique As Worksheet
Dim wbk_SD As Workbook
Dim ws_SD As Worksheet
'Dim wbk_PD as Excel.Workbook
'set and add later
'Dim ws_PD As Worksheet
'set and add later

Dim DataRange As Range
Dim iLastRow As Long
Dim iLastRow_unique As Long
Dim UniqueRng As Range
Dim Cell As Range
Dim LastRow As Long
Dim LastColumn As Long
Dim sfldr As String


Application.ScreenUpdating = False

'Note that the macro will save the pdf files in this active directory so you should save in an appropriate folder
DirectoryLocation = ActiveWorkbook.Path
sfldr = "C:\Separate Reports\"

'Set wbk_SD = Workbooks.Open(fDialog.SelectedItems(1))
Set wbk_SD = Workbooks.Open(sfldr & "HANA_-_Spending_Detail_Report_NARST_-_FT_08_15_19.xlsx")
Set wbk_worklist = Workbooks.Open(sfldr & "SDPD worklist VBA.xlsm")
'Set wbk_PD = Excel.Workbooks("ZANALYSIS_PATTERN PDF source")
'add later
Set ws_SD = wbk_SD.Sheets("Spending Detail Report") 'Amend to reflect the sheet you wish to work with
Set ws_unique = wbk_worklist.Sheets("AllARTnoBP") 'Amend to reflect the sheet you wish to work with

'Find the last row in each worksheet
iLastRow = ws_SD.Cells(Rows.Count, "E").End(xlUp).Row
'changed to column E
iLastRow_unique = ws_unique.Cells(Rows.Count, "A").End(xlUp).Row
'left as column A for Funded Program

With ws_SD
'I've set my range to reflect my headers which are fixed for this report
Set DataRange = ws_SD.Range("$B$2:$O$" & iLastRow)

'autofilter field is 5 as I want to print based on the value in column E
DataRange.AutoFilter Field:=5

'eventually will need to check for data in two columns to determine which source data file to call

Set UniqueRng = ws_unique.Range("A1:A" & iLastRow_unique)
For Each Cell In UniqueRng
DataRange.AutoFilter Field:=5, Criteria1:=Cell

Name = DirectoryLocation & "\" & Cell.Value & " _SD" & ".pdf"

ws_SD.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Name _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

Next Cell

End With
With ws_SD
.Protect Userinterfaceonly:=True, _
DrawingObjects:=False, Contents:=True, Scenarios:= _
True, AllowFormattingColumns:=True, AllowFormattingRows:=True
.EnableOutlining = True
.EnableAutoFilter = True
If .FilterMode Then
.ShowAllData
End If
End With
Application.ScreenUpdating = True

End Sub
 

Attachments

  • Screenshot 2021-10-13 092954.jpg
    Screenshot 2021-10-13 092954.jpg
    65.8 KB · Views: 22
  • Screenshot 2021-10-13 093131.jpg
    Screenshot 2021-10-13 093131.jpg
    29.7 KB · Views: 36

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Found the error...by hovering over the criteria1 it showed I had the header in my worklist included
Set UniqueRng = ws_unique.Range("A1:A" & iLastRow_unique)
changed to
Set UniqueRng = ws_unique.Range("A2:A" & iLastRow_unique)
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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