VBA Renaming Electronic Files

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any suggestions which I will provide feedback.

I am making an Excel file where I place the directory path (Cell B3) of where files that I may want to rename are located.

It first obtains the file names and places them in Column A starting with Row 5.

I then place the new names in Column B starting in Row 5.

I get the error when I try to run the Macro to rename "Run Time Error 53: File not Found"

Code:
Name DirFolderRename & CurrentName As DirFolderRename & NewName


The following is the entire code to rename the files:
Code:
Sub RenameFile()


'Dimensioning
    Dim DirFolderRename As String
    Dim CurrentName As String
    Dim NewName As String
    Dim i As Integer
    
    i = 5
    
    DirFolderRename = Sheets("List").Range("B3").Value
    
    Do While Sheets("List").Cells(i, 1).Text <> "" And Sheets("List").Cells(i, 2).Text <> ""
    
        CurrentName = Sheets("List").Cells(i, 1).Text <> ""
        NewName = Sheets("List").Cells(i, 2).Text <> ""
        Name DirFolderRename & CurrentName As DirFolderRename & NewName
        
        i = 1 + 1
        
    Loop
    
    MsgBox ("Complete")
    
    
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Both these lines will give you a value of true or false, depending on if the cell is empty
Code:
        CurrentName = Sheets("List").Cells(i, 1).Text <> ""
        NewName = Sheets("List").Cells(i, 2).Text <> ""
Remove this part from both of them
Code:
<> ""
 
Upvote 0
Thanks so much Fluff! That worked, but only the first file get renames. It executes on line 5 where Cell(5, 1) has the old file name and Cell(5, 2) has the new file name and then says complete.

Any idea why it's not looping through?
 
Upvote 0
This
Code:
 i = 1 + 1
should be
Code:
 i = [COLOR=#ff0000]i[/COLOR] + 1
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
I appologize.

This is strange, because it worked for a second and then now I'm getting the same error. Any idea why?

"Run-time error '53': File Not Found"

Code:
 Name DirFolderRename & CurrentName As DirFolderRename & NewName

Full Code

Code:
Sub RenameFile()


'Dimensioning
    Dim DirFolderRename As String
    Dim CurrentName As String
    Dim NewName As String
    Dim i As Integer
    
    i = 5
    
    DirFolderRename = Sheets("List").Range("B3").Value
     
    Do While Sheets("List").Cells(i, 1).Text <> "" And Sheets("List").Cells(i, 2).Text <> ""
    
        CurrentName = Sheets("List").Cells(i, 1).Text
        NewName = Sheets("List").Cells(i, 2).Text
        Name DirFolderRename & CurrentName As DirFolderRename & NewName
        
        i = i + 1
        
    Loop
    
    MsgBox ("Complete")
    
    
End Sub
 
Upvote 0
If you've already run the macro, then the files in col A will no longer exists.
 
Upvote 0
True, but I was running it on a different directory where the file names did not change.
 
Upvote 0
In that case check the values of CurrentName & DirFolderRename when you get the error. Then check that the file exists
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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