Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Excel VBA: rename folders based on columns

Hello Experts,

I have the following procedure which allows me to rename folders:
Sub Rename_Folders()
Const FILEPATH As String = "C:\"
Dim strfile As String
Dim filenum As String
strfile = Dir(FILEPATH)
Dim lr As Long
Dim ws As Worksheet
Dim old_name, new_name As String
Set ws = ActiveSheet
lr = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 2 To lr
''strOldDirName = FILEPATH & Sheets(1).Cells(i, 1).Value
''strNewDirName = FILEPATH & Sheets(1).Cells(i, 3).Value
strOldDirName = ws.Cells(i, "A").Value
strNewDirName = ws.Cells(i, "B").Value
Name strOldDirName As strNewDirName
Next i
End Sub

Open in new window


I would like to properly manage errors such as:
1-If folder reported in column A doesn’t exist skip the cell, display msbgox “Folder cannot renamed as it doesn’t exist” and continue with the next cell
2-If target folder reported in column B cannot be renamed, display msgbox “Folder cannot be renamed”

Please find attached example file.

If you have questions, please contact me.
Rename-folders-based-on-columns.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Ejgil Hedegaard
Ejgil Hedegaard
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

It works! Thank you very much for your help!!