I was on a call recently where somebody asked if it was possible to create a unique PDF for each item in a list. The answer is “Yes”, so in this post, I want to show you how to do it; how to create multiple PDFs based on a list in Excel.
Download the example file
I recommend you download the example file for this post. Then you’ll be able to work along with examples and see the solution in action, plus the file will be helpful for future reference.
Download the file: 0073 Save PDFs based on a list.zip
Watch the video
The scenario we are looking at is reasonably straightforward, so you should be able to adapt it to your specific circumstances.
In the example file, we want to create an exam report for each student based on their ID number (see screenshot below).
When the Student ID value in Cell G4 changes, the report recalculates to return the results for the selected student.
Cell G4 is based on a data validation list, using the values starting in Cell I4.
There is a shape labeled Save PDFs; we will assign the macro to this button later.
The entire report is based on data contained in an Excel Table (see screenshot below)
I have used XLOOKUP, UNIQUE, and FILTER functions for ease of demonstration. Therefore, the example file will only function correctly if you have a dynamic array-enabled version of Excel (i.e., it won’t work on Excel 2019 and earlier). However, I have not used any dynamic array functionality in the VBA macro. You should be able to adapt the code, even if you do not have a dynamic array-enabled version of Excel.
Insert the code below into a standard code module.
Sub SavePDFsFromList() 'Declare the Variables Dim ws As Worksheet Dim rngID As Range Dim rngListStart As Range Dim rowsCount As Long Dim i As Long Dim pdfFilePath As String Dim tempPDFFilePath As String 'Stop the screen updating while running Application.ScreenUpdating = False 'Reference Report Sheet Set ws = ActiveWorkbook.Sheets("Report") 'Reference the Student ID cell Set rngID = ws.Range("G4") 'Reference the start of the Studend ID List Set rngListStart = ws.Range("I4") 'Count the rows in the Student ID List rowsCount = rngListStart.CurrentRegion.Rows.Count - 1 'Create the PDF File Name pdfFilePath = "C:\Test Folder\PDF Export\Student Exam Record - [ID].pdf" For i = 1 To rowsCount 'Change the current student ID rngID.Value = rngListStart.Offset(i - 1, 0).Value 'Replace [ID] with the Student ID Value tempPDFFilePath = Replace(pdfFilePath, "[ID]", rngID.Value) 'Create the PDF ws.ExportAsFixedFormat Type:=xlTypePDF, _ fileName:=tempPDFFilePath Next i 'Restart the screen updating Application.ScreenUpdating = True End Sub
Adapting the code to your scenario
There are too many scenarios where this type of solution could be applied. Therefore, you will need to adapt the code above to meet your specific requirements. The essential items to change are:
Set rngID = ws.Range("G4")
The cell reference G4 should be changed to be the parameter that causes the report to change.
Set rngListStart = ws.Range("I4")
The cell reference I4 should be the first cell in the list of items to loop through.
pdfFilePath = "C:\Test Folder\PDF Export\Student Exam Record - [ID].pdf"
The pdfFilePath should contain a valid folder path on your PC or network.
The string of [ID] identifies the reference that should be replaced within the PDF file path. This text string needs to be the same in the code above and below.
tempPDFFilePath = Replace(pdfFilePath, "[ID]", rngID.Value)
Testing it out
Now, let’s assign the macro to the shape so we can test it out.
- Right-click on the shape
- Select Assign Macro… from the menu
- In the Assign Macro dialog box, select the macro
- Click OK
Everything is now set up; let’s test it out. Click the Save PDFs button.
We have the individual student reports all saved down in a few seconds. So even if you had hundreds, it really shouldn’t take long 😀.
If you want to look deeper into the topic of saving PDFs with VBA macros, then check out this post: Save Excel as PDF with VBA.
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: