Rename files from old name in column C and new name in column D

KlausW

Active Member
Joined
Sep 9, 2020
Messages
458
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone

Anyone who can help, I use this VBA code to rename all files in a folder / subfolder.
Folder in Cell H1
But now I would like the old name to be in column C2 and down and the new name in column D2 and down. The new files must have the same file format as before it was renamed.
Anyone who can help?
Any help will be appreciated

Best Regards
Klaus W

VBA Code:
Sub Rename_Files()

Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets("Sheet1")

Dim fso As New FileSystemObject

Dim fo As Folder

Dim f As File

Dim new_name As String

Set fo = fso.GetFolder(sh.Range("H1").Value)

For Each f In fo.Files

new_name = Application.VLookup(f.Name, sh.Range("A:c"), 3, 0)

f.Name = new_name

Next

MsgBox "All files renamed successfully!"

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Assuming that the posted code works, here is a small edit to your code
VBA Code:
Sub Rename_Files()

Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets("Sheet1")

Dim fso As New FileSystemObject

Dim fo As Folder

Dim f As File

Dim new_name As String
Dim old_name As String

Set fo = fso.GetFolder(sh.Range("H1").Value)

For Each f In fo.Files

old_name = sh.Range("H1").Value

new_name = Application.VLookup(old_name, sht.Range("A:C"),3,0)

f.Name = new_name

Next

MsgBox "All files renamed successfully!"

End Sub
 
Upvote 0
With the old name in Column C and the new one in Column D, try...

VBA Code:
new_name = Application.VLookup(f.Name, sh.Range("C:D"), 2, 0)

or

VBA Code:
    With Application
        new_name = .Index(sh.Range("D:D"), .Match(f.Name, sh.Range("C:C"), 0))
    End With

Hope this hleps!
 
Last edited:
Upvote 0
Solution
With the old name in Column C and the new one in Column D, try...

VBA Code:
new_name = Application.VLookup(f.Name, sh.Range("C:D"), 2, 0)

or

VBA Code:
    With Application
        new_name = .Index(sh.Range("D:D"), .Match(f.Name, sh.Range("C:C"), 0))
    End With

Hope this hleps!
Hi Domenic, thank you, it should be just as it should be. Good day greetings from Denmark Klaus W
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,189
Members
452,893
Latest member
denay

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