Rename Files in Folder using VBA

Poppaby

New Member
Joined
Nov 19, 2020
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi all

I have a folder with a large number of files. I have listed them all in a column OldName. I want to rename them to the name in column NewName. The directory is listed at the top

1623240267774.png


Any help would be appreciated


Additionally, each file has a single sheet called Sheet1. I would like to add a column after the last column containing data, populated with the new file name as the column header. So the file listed in row 4 of the picture above would have a new column added with 20001Allowances01 as the header

Dave
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You have really got two entirely different questions here. this code should rename your files.
VBA Code:
Sub rename()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(LastRow, 10))
pathn = inarr(1, 1)
For i = 3 To LastRow
'You can change the path and file name
    src = pathn & "\" & inarr(i, 1)
    dst = pathn & "\" & inarr(i, 1)
     Name src As dst
Next i
End Sub
 
Upvote 0
whoops an error try this:
VBA Code:
Sub rename()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(LastRow, 10))
pathn = inarr(1, 1)
For i = 3 To LastRow
'You can change the path and file name
    src = pathn & "\" & inarr(i, 1)
    dst = pathn & "\" & inarr(i, 2)
     Name src As dst
Next i
End Sub
 
Upvote 0
Thanks - I am getting an error message saying file not found
 
Upvote 0
When you get the error go into debug and hover your mouse over the "src" variable and see what file it is showing this is the file that it can't find.
 
Upvote 0
Hi,​
should obviously be :​
Rich (BB code):
For i = 4 To LastRow
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top