Create a PDF for Each Excel Row with a Custom File Name
Question details
Create a separate PDF for each Excel data row using a template and a file name based on a selected cell.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A workbook contains a data sheet and a template that needs to be populated and exported for each record.
- Observed behavior
- Each record is placed in the template and exported as an individually named PDF.
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) in a local folder, and enable macros if prompted. Set the template's print area and page layout before exporting, then test the macro with one data row.
Export each data row as a PDF with an Excel VBA macro
Use this macro when each data row should populate the same template and produce a separate PDF in the workbook's folder.
This example expects sheets named Data and Template. Data row 1 contains headers; columns A, B, and C contain values to place in Template cells B2, B3, and B4. Column A also supplies the file name.
The macro prefixes each file name with Record_ and adds the source row number. It replaces invalid file-name characters and skips files that already exist.
Rename the worksheets Data and Template. Put a header in row 1 of Data and at least one test record in columns A–C starting at row 2. On Template, use Page Layout > Print Area to set the portion to export.
In Excel, open Developer > Visual Basic, select the workbook, and choose Insert > Module. If the Developer tab is hidden, enable it in Excel's ribbon settings.
Paste the following code into the module. Adjust the sheet names, column references, and template cell addresses to match your workbook: Sub ExportRowsToPDF() Dim wb As Workbook Dim dataSheet As Worksheet, templateSheet As Worksheet Dim lastRow As Long, r As Long, i As Long Dim recordID As String, safeID As String, ch As String Dim pdfPath As String Dim exported As Long, skipped As Long On Error GoTo Failed Set wb = ThisWorkbook If wb.Path = "" Then MsgBox "Save the workbook in a local folder first." Exit Sub End If Set dataSheet = wb.Worksheets("Data") Set templateSheet = wb.Worksheets("Template") lastRow = dataSheet.Cells(dataSheet.Rows.Count, "A").End(xlUp).Row For r = 2 To lastRow If Not IsError(dataSheet.Cells(r, "A").Value) Then recordID = Trim(CStr(dataSheet.Cells(r, "A").Value)) If recordID <> "" Then safeID = "" For i = 1 To Len(recordID) ch = Mid(recordID, i, 1) If InStr(1, "\/:*?""<>|", ch) > 0 Or (AscW(ch) >= 0 And AscW(ch) < 32) Then safeID = safeID & "_" Else safeID = safeID & ch End If Next i pdfPath = wb.Path & Application.PathSeparator & "Record_" & safeID & "_" & r & ".pdf" If Dir(pdfPath) = "" Then templateSheet.Range("B2").Value = dataSheet.Cells(r, "A").Value templateSheet.Range("B3").Value = dataSheet.Cells(r, "B").Value templateSheet.Range("B4").Value = dataSheet.Cells(r, "C").Value Application.Calculate templateSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath exported = exported + 1 Else skipped = skipped + 1 End If End If End If Next r MsgBox exported & " PDF(s) exported; " & skipped & " existing file(s) skipped." Exit Sub Failed: MsgBox "Export stopped: " & Err.Description End Sub
Save the workbook, then choose Developer > Macros, select ExportRowsToPDF, and click Run. Open the PDF in the workbook's folder and check its contents, layout, and file name before adding or processing the remaining records.

Work with Excel data and export PDFs in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for working with Excel files and exporting sheets to PDF. Open compatible workbooks in WPS Spreadsheets for an easy transition; check VBA support in your edition before relying on an Excel macro.

Frequently Asked Questions
How do I use a different column for the PDF file name?
In the VBA module, change the column A references used for recordID to the column containing your identifier. Also update the lastRow reference if that column should determine the final record.
Where are the PDFs saved?
The example saves PDFs in the same folder as the workbook using ThisWorkbook.Path. Save the workbook to a local folder before running the macro.
Why was an existing PDF not replaced?
The macro checks the destination with Dir and skips an existing file. Move or rename that PDF before running the macro again if you need a new export.
How can I change what appears in each PDF?
Change the Template cell addresses and Data column references in the macro. Use Page Layout > Print Area on the Template sheet to control which cells are exported.




