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
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442
Try this and see if the message box gives you the correct folders
Code:
Sub ChangeInFolder()
    Dim OldName As String
    Dim NewName As String
    Dim LastRow As Long
    Dim i As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To LastRow
        OldName = Range("A" & i)
        NewName = Range("C" & i)
        MsgBox OldName & " to" & vbCr & NewName
        Name OldName As NewName
    Next i
End Sub

PS I was wrong about the backslahes earlier, it does work with those included, my apologies.
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
yes i using the original one and it is on my pc.

it say run=time error 75
Path/File access error


Don't use the code in post #6 , it's wrong for what you are trying to do. Stick with the original code.
 

harky

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

I had error @ Name OldName As NewName

Try this and see if the message box gives you the correct folders
Code:
Sub ChangeInFolder()
    Dim OldName As String
    Dim NewName As String
    Dim LastRow As Long
    Dim i As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To LastRow
        OldName = Range("A" & i)
        NewName = Range("C" & i)
        MsgBox OldName & " to" & vbCr & NewName
        Name OldName As NewName
    Next i
End Sub

PS I was wrong about the backslahes earlier, it does work with those included, my apologies.
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442
Make a directory on C:\, say C:\MyTempFolder

then try using C:\MyTempFolder in "A" and C:\MyTempFolderNew in "C" on the first row of your sheet
 

harky

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

ADVERTISEMENT

I had no issue with code in post #6 but it only rename 1 folder.
 

harky

Active Member
Joined
Apr 8, 2010
Messages
316
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
same issue..... think the code dont work

Make a directory on C:\, say C:\MyTempFolder

then try using C:\MyTempFolder in "A" and C:\MyTempFolderNew in "C" on the first row of your sheet
 

harky

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

ADVERTISEMENT

I manage to get it work somehow...

ABC
C:\Users\ABC\Desktop\TEST\AAA1C:\Users\ABC\Desktop\ABC\1111

<tbody>
</tbody>



Code:
Sub Rename_Folder()
Dim OldName As String
Dim NewName As String
Dim LastRow As Long
Dim I As Long




    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    For I = [COLOR=#ff0000]2[/COLOR] To LastRow
        OldName = Range("A" & I).Value
        NewName = Range("C" & I).Value
        Name OldName As NewName
    Next I
End Sub


Make a directory on C:\, say C:\MyTempFolder

then try using C:\MyTempFolder in "A" and C:\MyTempFolderNew in "C" on the first row of your sheet
 
Last edited:

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
442
Great. The 2 means it starts on the second row (you may have headers!) rather than the first row. The message box would have picked that up!
 

Watch MrExcel Video

Forum statistics

Threads
1,128,128
Messages
5,628,862
Members
416,345
Latest member
sayad

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