Excel VBA to rename filesname or folder

harky

Active Member
Joined
Apr 8, 2010
Messages
288
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
441
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
288
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
441
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
288
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
288
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
441
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
441
Don't use the code in post #6 , it's wrong for what you are trying to do. Stick with the original code.
 

Forum statistics

Threads
1,078,437
Messages
5,340,278
Members
399,362
Latest member
iayb

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top