Skipping rows in loop

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
191
I have a macro that renames files based on the contents of two ranges.
i.e 'Column A' contains the original name and 'Column E' will be the new name.
When I run the macro it crashes if I haven't renamed an item in the 'Column E'.

Is there a way to skip a cell if it is the same in both ranges?


Thanks

VBA Code:
Sub RenameFiles()

Dim myPath As String
myPath = Sheet1.[address1]

r = 2
Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 5))
    Name myPath & Cells(r, 1).Value As myPath & Cells(r, 5).Value
    r = r + 1
Loop
MsgBox "Successful"

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this.
VBA Code:
Sub RenameFiles()
Dim myPath As String
    myPath = Sheet1.[address1]

    r = 2

    Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 5))
        If Cells(r,1).Value <> Cells(r,5).Value Then
            Name myPath & Cells(r, 1).Value As myPath & Cells(r, 5).Value
        End If
        r = r + 1
    Loop 

    MsgBox "Successful"

End Sub
 
Upvote 0
Solution
Try this.
VBA Code:
Sub RenameFiles()
Dim myPath As String
    myPath = Sheet1.[address1]

    r = 2

    Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 5))
        If Cells(r,1).Value <> Cells(r,5).Value Then
            Name myPath & Cells(r, 1).Value As myPath & Cells(r, 5).Value
        End If
        r = r + 1
    Loop

    MsgBox "Successful"

End Sub
Thanks @Norie, that worked.
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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