Reputation: 3256
I am trying to compare two workbooks to pick out any rows that are dissimilar between them. I have created the code below to do this, but I was hoping there was something similar to a continue
which I could use inside of the if-statement to exit the innermost for each
-loop once a difference is found. Is there any obvious way to do this? I guess I could use a goto
-statement, but I'd prefer to avoid this.
Sub test()
Dim wb_new As Workbook, wb_old As Workbook
Dim ws As Worksheet
Dim r As Range, c_old As Range, c_new As Range
Dim i As Long
Set wb_new = Application.Workbooks("Book1.xlsx")
Set wb_old = Application.Workbooks("Book2.xlsx")
For Each ws In wb_old.Worksheets
For i = 0 To 275
Set r = ws.Range("A1:Q1").Offset(i, 0)
For Each c_old In r
Set c_new = wb_new.Worksheets(ws.Name).Range("A1").Offset(i, c_old.Column - 1)
If c_new <> c_old Then
Debug.Print c_new.Address
End If
Next c_old
Next i
Next ws
End Sub
Upvotes: 0
Views: 46
Reputation: 16015
You seem to have already answered your own question in your title - simply use Exit For
.
Here is a reference: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/exit-statement
Upvotes: 1
Reputation:
Yes, there is an Exit For
statement.
If c_new <> c_old Then
Debug.Print c_new.Address
Exit For 'exits the inner loop
End If
Upvotes: 2