Skipping rows in loop

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
182
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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
182
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.
 

Forum statistics

Threads
1,136,290
Messages
5,674,886
Members
419,532
Latest member
longphamtel

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
Top