VBA & Macro Problems

Rename PDF Files Using Excel Column Values with VBA

Phi Hung VoPhi Hung Vo Nov 22, 2026 869 views

Question details

Rename PDF files in a folder using replacement names listed in an Excel column.

Rename PDF Files Using Excel Column Values with VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
Each row in column A contains a replacement name for a PDF, matched to source files sorted by filename.
Observed behavior
The user wants the PDFs renamed in the intended order with a .pdf extension and without overwriting existing files.
Before you start

Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) in the PDF folder. Put one destination name per row in column A, starting at A1 with no header or blank rows, and check that the sorted source filenames correspond to those rows.

Solution 1Recommended

Sort and rename PDFs with an Excel VBA macro

Use this macro when the workbook and PDFs are in the same folder and column A contains one new name for each file.

The macro sorts filenames alphabetically, not in natural numeric order: file10.pdf comes before file2.pdf. Use zero-padded source names such as file02.pdf and file10.pdf if numeric order matters.

The macro stops before renaming if the file count differs from the number of rows, a destination name is duplicated, or a changed destination already exists. It does not support swapping existing filenames.

1
Check the worksheet

Open the workbook in desktop Excel and select the sheet containing the replacement names in column A. Remove any header and confirm that the row order matches the alphabetical order of the PDF filenames.

2
Add the macro

Press Alt+F11, choose Insert > Module, and paste this code into the module: Sub RenamePDFsFromColumnA() Dim folder As String, f As String, temp As String Dim files() As String, targets() As String Dim n As Long, lastRow As Long, i As Long, j As Long Dim nm As String, badChars As Variant, c As Variant Dim changed As Long On Error GoTo RenameError If ThisWorkbook.Path = "" Then MsgBox "Save this workbook in the PDF folder first." Exit Sub End If folder = ThisWorkbook.Path & Application.PathSeparator f = Dir(folder & "*.pdf") Do While f <> "" n = n + 1 ReDim Preserve files(1 To n) files(n) = f f = Dir() Loop If n = 0 Then MsgBox "No PDFs found in the workbook folder." Exit Sub End If For i = 1 To n - 1 For j = i + 1 To n If StrComp(files(i), files(j), vbTextCompare) > 0 Then temp = files(i): files(i) = files(j): files(j) = temp End If Next j Next i lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row If lastRow <> n Then MsgBox "The number of column A rows does not match the PDF count." Exit Sub End If ReDim targets(1 To n) badChars = Array("\", "/", ":", "*", "?", Chr(34), "<", ">", "|") For i = 1 To n nm = Trim(CStr(ActiveSheet.Cells(i, "A").Value)) If Len(nm) >= 4 Then If LCase(Right(nm, 4)) = ".pdf" Then nm = Left(nm, Len(nm) - 4) End If nm = Trim(nm) If nm = "" Or Right(nm, 1) = "." Then MsgBox "Invalid or blank name in row " & i Exit Sub End If For Each c In badChars If InStr(nm, CStr(c)) > 0 Then MsgBox "Invalid character in row " & i Exit Sub End If Next c targets(i) = nm & ".pdf" For j = 1 To i - 1 If StrComp(targets(i), targets(j), vbTextCompare) = 0 Then MsgBox "Duplicate destination name in row " & i Exit Sub End If Next j If StrComp(files(i), targets(i), vbTextCompare) <> 0 Then If Dir(folder & targets(i), vbNormal Or vbReadOnly Or vbHidden Or vbSystem) <> "" Then MsgBox "Destination already exists: " & targets(i) Exit Sub End If End If Next i For i = 1 To n If StrComp(files(i), targets(i), vbTextCompare) <> 0 Then Name folder & files(i) As folder & targets(i) changed = changed + 1 End If Next i MsgBox "Renamed " & changed & " PDF file(s)." Exit Sub RenameError: MsgBox "Stopped after renaming " & changed & " file(s): " & Err.Description End Sub

3
Run and verify

Return to the sheet with column A selected as the active worksheet. Open Developer > Macros, select RenamePDFsFromColumnA, and click Run. Check the filenames in the PDF folder after the completion message.

Sort and rename PDFs with an Excel VBA macro
If the macro stops during renaming: The message reports how many files were renamed before the error. Resolve the reported issue and reconcile the folder and worksheet before running it again; the macro does not undo completed renames.
Free Microsoft Office alternative

Prepare PDF naming lists in WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for editing spreadsheet-based naming lists. Open compatible Excel files and migrate your worksheets easily; the Excel VBA macro above requires a compatible VBA environment and is not guaranteed to run in WPS.

Organize and review PDF replacement names in a spreadsheetOpen and edit Microsoft Excel formats such as XLSXUse a familiar spreadsheet interfaceWork with a free, lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Does the macro match files in numeric order?

No. It sorts filenames alphabetically, so file10.pdf precedes file2.pdf. Use zero-padded filenames, such as file02.pdf and file10.pdf, or adjust the naming list to match the alphabetical order.

Should I include .pdf in column A?

Either is acceptable. The macro removes a .pdf suffix if present and then adds .pdf to the destination name.

What happens if a destination filename already exists?

The macro stops before making changes if a changed destination already exists. Choose a unique destination name in column A and run the macro again.

Can this macro swap the names of two existing PDFs?

No. Its preflight check treats another existing source filename as a conflict. Use a separate two-stage renaming process with unique temporary names for swaps.

Why did the macro stop after renaming some files?

A file permission issue, an unsupported filename, or another operating-system error can interrupt the rename loop. Read the error message and check the folder and column A before trying again; completed renames are not automatically reversed.