Excel VBA to rename filesname or folder

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hi Any idea why this not working? :D

Code:
Sub ChangeInFolder()
Dim OldName As String
Dim NewName As String
Dim LastRow As Long
Dim i As Long
LastRow = Range("A65536").End(xlUp).Row


For i = 1 To LastRow
    OldName = Range("A" & i).Value
    NewName = Range("C" & i).Value
    Name OldName As NewName
    
Next i


Application.ScreenUpdating = True
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Check that the OldNames are valid (eg C:\Users\HarryPotter\Documents\Wizard.xlsm) and that they exist.
Check that the NewNames are valid and they don't exist

Edit: Also check you have permission to write to the location(s)
 
Last edited:
Upvote 0
oh thanks. Just notice is dont rename folder.

I want to rename folder not file in the folder

Check that the OldNames are valid (eg C:\Users\HarryPotter\Documents\Wizard.xlsm) and that they exist.
Check that the NewNames are valid and they don't exist

Edit: Also check you have permission to write to the location(s)
 
Upvote 0
The folders should be renamed as long as the names are valid and no files are in use in them.
 
Last edited:
Upvote 0
i mean the code dont work for rename folder

i put

A
C:\Users\ABC\Desktop\TEST\TEST1\


C
C:\Users\ABC\Desktop\TEST\ABC1\

it dont work.

The folders should be renamed as long as the names are valid and no files are in use in them.
 
Upvote 0
i found another code.

It only work for row 1 only

Code:
Sub rename_folder()


Dim old_name, new_name As String


For I = 2 To Sheets(1).Range("A1").End(xlDown).Row


new_name = Left(Sheets(1).Cells(I, 1).Value, Len(Sheets(1).Cells(I, 1).Value) - Len(Sheets(1).Cells(I, 2).Value))


new_name = new_name & Sheets(1).Cells(I, 3).Value


old_name = Sheets(1).Cells(I, 1).Value
Name old_name As new_name


Next I


End Sub
 
Upvote 0
They are not valid folders with a backslash at the end.

Try C:\Users\ABC\Desktop\TEST\TEST1 to C:\Users\ABC\Desktop\TEST\ABC1
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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