Rename PNG Files from Excel Cell Values Using VBA
Question details
Rename PNG files in a folder using original and revised filenames listed in an Excel worksheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user has original PNG filenames in one column and corresponding new filenames in another.
- Observed behavior
- Each file should be renamed to the filename on the same worksheet row.
Put the original filenames in column A and the corresponding new filenames in column B, including the .png extension. Use row 1 for headers, leave column C available for results, and test the macro on copies of the files in a separate folder.
Run an Excel VBA macro to rename the PNG files
Use this macro to select a folder and rename matching files row by row, while recording skipped rows and errors.
The macro processes the active worksheet from row 2 to the last used row in column A. It checks the selected folder only, not its subfolders, and does not overwrite an existing destination file.
Open the workbook in desktop Excel, select the worksheet containing the filename list, and open Developer > Visual Basic. If Developer is hidden, enable it in Excel's ribbon settings.
In the VBA editor, choose Insert > Module and paste the following code: Option Explicit Sub RenamePngFiles() Dim ws As Worksheet Dim folder As String, oldName As String, newName As String Dim oldPath As String, newPath As String Dim lastRow As Long, r As Long Dim picker As FileDialog Set ws = ActiveSheet Set picker = Application.FileDialog(msoFileDialogFolderPicker) picker.Title = "Select the folder containing the PNG files" 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 r = 2 To lastRow On Error GoTo RowError oldName = Trim$(CStr(ws.Cells(r, "A").Value2)) newName = Trim$(CStr(ws.Cells(r, "B").Value2)) If Not ValidPngName(oldName) Or Not ValidPngName(newName) Then ws.Cells(r, "C").Value = "Skipped: invalid PNG filename" ElseIf StrComp(oldName, newName, vbTextCompare) = 0 Then ws.Cells(r, "C").Value = "Skipped: names are the same" Else oldPath = folder & oldName newPath = folder & newName If Dir$(oldPath, vbNormal Or vbHidden Or vbSystem Or vbReadOnly) = "" Then ws.Cells(r, "C").Value = "Skipped: original file not found" ElseIf Dir$(newPath, vbNormal Or vbHidden Or vbSystem Or vbReadOnly Or vbDirectory) <> "" Then ws.Cells(r, "C").Value = "Skipped: destination already exists" Else Name oldPath As newPath ws.Cells(r, "C").Value = "Renamed" End If End If ContinueRow: On Error GoTo 0 Next r MsgBox "Finished. Check column C for results." Exit Sub RowError: ws.Cells(r, "C").Value = "Error: " & Err.Description Err.Clear Resume ContinueRow End Sub Private Function ValidPngName(ByVal fileName As String) As Boolean Dim bad As String, i As Long bad = "\/:*?""<>|" If Len(fileName) <= 4 Or LCase$(Right$(fileName, 4)) <> ".png" Then Exit Function For i = 1 To Len(bad) If InStr(fileName, Mid$(bad, i, 1)) > 0 Then Exit Function Next i ValidPngName = True End Function
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Return to the filename worksheet, choose Developer > Macros, select RenamePngFiles, and click Run. Select the folder containing the PNG files.
Check column C for each row's result. Correct any missing filenames or destination conflicts in columns A and B before running the macro again.

Manage PNG filename lists with WPS Spreadsheet
WPS Office is a free, lightweight alternative for working with spreadsheet filename lists. Its familiar interface and Microsoft Office format compatibility make it easy to move between office suites. The Excel VBA macro above requires a compatible Excel environment; WPS is not a guaranteed fix for running it.

Frequently Asked Questions
Do the filenames need to include the PNG extension?
Yes. Enter the complete original filename in column A and the complete new filename in column B, including .png in both cells.
What happens if the new filename already exists?
The macro skips that row rather than overwriting the existing file. Check column C, then change the destination filename in column B.
Can the macro rename files in subfolders?
No. The macro checks only the folder selected in the folder picker. Run it separately for another folder, using the appropriate filename list.
Why was a row marked as an invalid PNG filename?
Check columns A and B for blank values, a missing .png extension, or characters such as slashes, colons, and question marks. Enter filenames only, not full paths.
Can I use Excel for the web to run this VBA macro?
No. Open the workbook in a desktop version of Excel that supports VBA, then use Developer > Macros to run it.




