Rename PDF Files from Excel Cell Values with VBA
Question details
Rename numbered PDF files using corresponding names listed in an Excel worksheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A folder contains numbered PDFs, and a worksheet maps each file to its desired name.
- Observed behavior
- Each numbered PDF should be renamed to the name specified in its corresponding worksheet row.
Prepare a test folder containing copies of a few PDFs. In the active worksheet, put each current filename, including .pdf, in column A and its desired new name in column B, starting on row 2; use text values to preserve leading zeros.
Run an Excel VBA macro to rename PDFs from worksheet rows
Use this method when each row explicitly maps an existing PDF filename to a new filename.
The macro asks you to select the PDF folder, then writes a result for each row in column C. It skips existing destination files rather than overwriting them.
In Excel, open the workbook and select the worksheet containing the filename mapping. Open Developer > Visual Basic, then choose Insert > Module.
Paste this code into the module: Sub RenamePDFsFromSheet() Dim ws As Worksheet, picker As FileDialog Dim folder As String, oldName As String, newName As String Dim src As String, dst As String, lastRow As Long, i As Long Set ws = ActiveSheet Set picker = Application.FileDialog(msoFileDialogFolderPicker) picker.Title = "Select the folder containing the PDFs" If picker.Show <> -1 Then Exit Sub folder = picker.SelectedItems(1) & Application.PathSeparator lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ws.Range("C1").Value = "Result" For i = 2 To lastRow On Error GoTo RowError oldName = Trim(CStr(ws.Cells(i, "A").Value2)) newName = Trim(CStr(ws.Cells(i, "B").Value2)) If oldName = "" Or newName = "" Then ws.Cells(i, "C").Value = "Skipped: missing name" GoTo NextRow End If If InvalidFileName(oldName) Or InvalidFileName(newName) Then ws.Cells(i, "C").Value = "Skipped: invalid filename" GoTo NextRow End If If LCase(Right(oldName, 4)) <> ".pdf" Then ws.Cells(i, "C").Value = "Skipped: source must end in .pdf" GoTo NextRow End If If LCase(Right(newName, 4)) <> ".pdf" Then newName = newName & ".pdf" src = folder & oldName dst = folder & newName If Dir(src) = "" Then ws.Cells(i, "C").Value = "Skipped: source not found" ElseIf StrComp(src, dst, vbTextCompare) = 0 Then ws.Cells(i, "C").Value = "Skipped: same name" ElseIf Dir(dst) <> "" Then ws.Cells(i, "C").Value = "Skipped: destination exists" Else Name src As dst ws.Cells(i, "C").Value = "Renamed" End If NextRow: On Error GoTo 0 Next i Exit Sub RowError: ws.Cells(i, "C").Value = "Error: " & Err.Description Resume NextRow End Sub Function InvalidFileName(ByVal fileName As String) As Boolean Dim ch As Variant For Each ch In Array("\", "/", ":", "*", "?", Chr(34), "<", ">", "|") If InStr(fileName, ch) > 0 Then InvalidFileName = True Exit Function End If Next ch End Function
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Return to the mapping worksheet, choose Developer > Macros, select RenamePDFsFromSheet, and click Run. Select the test folder when prompted.
Check column C and the selected folder. Correct missing names, existing destinations, or reported errors, then run the macro again on the intended folder after the test succeeds.

Manage PDF filename lists in WPS Spreadsheet
WPS Office is a free, lightweight alternative with a familiar interface for organizing filename mappings. It supports common Excel formats, making it easier to move existing worksheets between office suites; Excel VBA macro support may vary by WPS edition and configuration.

Frequently Asked Questions
How should I arrange the Excel worksheet?
Starting in row 2, enter the current PDF filename, including .pdf, in column A and the desired name in column B. The macro writes its result in column C.
Can the macro preserve leading zeros in filenames?
Yes. Format column A as Text before entering names such as 001.pdf, and enter the complete filename exactly as it appears in the folder.
What happens if the new PDF filename already exists?
The macro skips that row and reports “Skipped: destination exists” in column C. Choose a different target name before running it again.
Why does Excel report that a source file was not found?
Check that you selected the correct folder and that column A contains the exact filename and .pdf extension. The macro looks in the selected folder, not its subfolders.
Why can't I run the macro?
Save the workbook as .xlsm and check Excel's macro security settings. If a security warning appears, enable macros only when you trust the workbook and its code.




