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?
 

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
3,963
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
 

Forum statistics

Threads
1,082,145
Messages
5,363,389
Members
400,735
Latest member
amit Misra

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top