VBA & Macro Problems

Create a PDF for Each Excel Row with a Custom File Name

Elise WilliamsElise Williams Nov 18, 2026 869 views

Question details

Create a separate PDF for each Excel data row using a template and a file name based on a selected cell.

Create a PDF for Each Excel Row with a Custom File Name
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.
Before you start

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.

Solution 1Recommended

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.

1
Prepare the workbook

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.

2
Open the VBA editor

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.

3
Insert the macro

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

4
Run and check the test

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.

Export each data row as a PDF with an Excel VBA macro
Existing PDFs are skipped: The macro does not overwrite an existing file with the same name. Move or rename an earlier PDF if you want to export that record again. Rows with a blank or error value in column A are ignored.
Free Microsoft Office alternative

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.

Edit spreadsheet data and export a sheet to PDFCompatible with Microsoft Excel formats including XLSX and XLSMFamiliar spreadsheet interface for moving from Microsoft OfficeFree and lightweight for everyday document work
microsoft office alternative - wps office

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.