VBA Batch File Renaming

rachelml1130

New Member
Joined
Jun 16, 2016
Messages
2
Hello,

I am attempting to batch rename files using the following macro created in VBA:

Sub RenameFile()
Dim z As String
Dim s As String
Dim V As Integer
Dim TotalRow As Integer

TotalRow = ActiveSheet.UsedRange.Rows.Count

For V = 1 To TotalRow

' Get value of each row in columns 1 start at row 2
z = Cells(V + 1, 1).Value
' Get value of each row in columns 2 start at row 2
s = Cells(V + 1, 2).Value

Dim sOldPathName As String
sOldPathName = z
'On Error Resume Next
Name sOldPathName As s

Next V

MsgBox "Congratulations! You have successfully renamed all the files"

End Sub

I keep getting an error when I run it though on the underlined line. But the naming shows as correct. I have tried a different but similar one as well and still have the same error on the same line. Does anyone know what I might have wrong here?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the forum

I think I do (what might be wrong here?) :)

change
Code:
 For V = 1 to Totalrow
to
Code:
For V = 1 to Totalrow-1
and you should be fine

Because in your code, you use cells(V+1,1), so by the rime V=Totalrow, say 10 for example, V+1 will be 11 and row 11 is a blank row which makes the Name statement return and error 'cos the Name statement sees Name "blank" as "blank", something like that

or make your code this way
Code:
Sub RenameFile()
    Dim z As String
    Dim s As String
    Dim V As Integer
    Dim TotalRow As Integer
    
    TotalRow = ActiveSheet.UsedRange.Rows.Count
    
    For V = 2 To TotalRow
    
        ' Get value of each row in columns 1 start at row 2
        z = Cells(V, 1).Value
        ' Get value of each row in columns 2 start at row 2
        s = Cells(V, 2).Value
    
        Dim sOldPathName As String
        sOldPathName = z
        'On Error Resume Next
        Name sOldPathName As s
        
    Next V
    
    MsgBox "Congratulations! You have successfully renamed all the files"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,064
Members
448,941
Latest member
AlphaRino

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