Excel VBA to rename filesname or folder

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442
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:

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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)
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442
The folders should be renamed as long as the names are valid and no files are in use in them.
 
Last edited:

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  2. 2019
Platform
  1. Windows

ADVERTISEMENT

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.
 

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442

ADVERTISEMENT

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
 

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
cannot. has error

@ Name OldName As NewName

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
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442
Don't use the code in post #6 , it's wrong for what you are trying to do. Stick with the original code.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,207
Messages
5,629,294
Members
416,384
Latest member
frsamiee

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
Top