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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,298
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,244
Messages
5,657,582
Members
418,401
Latest member
B_A_M155

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