Forum Discussion
PatMyer
Feb 24, 2023Copper Contributor
How to rename file names from an excel cell
I have a list of .png file names exported to excel that I have modified (added an _ in a specific location in the name) and want to rename the same .png file names in the same folder I pulled from to...
- Feb 24, 2023
Oh wait - my bad. The values in the worksheet already include .png.
Try this version:
Sub RenameFiles() ' Change the path as needed, but keep the trailing backslash Const sFolder = "C:\MyFiles\" Dim r As Long Dim m As Long Dim v As Variant m = Range("A" & Rows.Count).End(xlUp).Row v = Range("A1:B" & m).Value On Error GoTo ErrHandler For r = 2 To m Name sFolder & v(r, 1) As sFolder & v(r, 2) Next r Exit Sub ErrHandler: MsgBox "Failed to rename " & v(r, 1), vbInformation Resume Next End Sub
HansVogelaar
Feb 24, 2023MVP
Oh wait - my bad. The values in the worksheet already include .png.
Try this version:
Sub RenameFiles()
' Change the path as needed, but keep the trailing backslash
Const sFolder = "C:\MyFiles\"
Dim r As Long
Dim m As Long
Dim v As Variant
m = Range("A" & Rows.Count).End(xlUp).Row
v = Range("A1:B" & m).Value
On Error GoTo ErrHandler
For r = 2 To m
Name sFolder & v(r, 1) As sFolder & v(r, 2)
Next r
Exit Sub
ErrHandler:
MsgBox "Failed to rename " & v(r, 1), vbInformation
Resume Next
End Sub
PatMyer
Feb 24, 2023Copper Contributor
It worked, yes.... Thank you so much.
I wish computers were not so boring when I was in school (graduated in '81). This stuff is very interesting, just wish it just didn't fly over my head all the time when I try to learn it now.
HAVE A GREAT WEEKEND AND AGAIN, THANKS... Oh, I will "Mark as best response" now.
I wish computers were not so boring when I was in school (graduated in '81). This stuff is very interesting, just wish it just didn't fly over my head all the time when I try to learn it now.
HAVE A GREAT WEEKEND AND AGAIN, THANKS... Oh, I will "Mark as best response" now.
- HansVogelaarFeb 24, 2023MVP
Thanks!