我试图比较两个工作簿来挑选它们之间不同的任何行。我已经创建了下面的代码来执行此操作,但我希望有一些类似于continue
的内容,我可以在if语句中使用它来退出最内层的for each
- 循环找到。有没有明显的方法来做到这一点?我想我可以使用goto
语句,但我更愿意避免这种情况。
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
答案 0 :(得分:2)
是的,有Exit For
声明。
If c_new <> c_old Then
Debug.Print c_new.Address
Exit For 'exits the inner loop
End If
答案 1 :(得分:1)
您似乎已经在标题中回答了自己的问题 - 只需使用Exit For
。
这是一个参考: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/exit-statement