【发布时间】:2019-06-08 10:37:42
【问题描述】:
我同时拥有 next 和 for,但我仍然收到此错误消息说“next without for”。有人可以帮我解决这个错误信息吗?我在这里做错了什么?
Sub CheckCOA()
new_coa_detected = 0
Sheets("CAO & other").Select
With ActiveSheet
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
MsgBox "Ref Table Length = " & lr
End With
Sheets("Month1").Select
With ActiveSheet
lr1 = .Cells(.Rows.Count, "A").End(xlUp).Row
MsgBox "Month 1 Length = " & lr1
End With
For l = 2 To lr1
code = Cells(l, 1)
Sheets("CAO & other").Select
For m = 2 To lr
If Cells(m, 1) = code Then
GoTo existing_code
End If
Next m
new_code:
'New code detected
Sheets("NewCOAsDetected").Select
With ActiveSheet
lr_new_coa = .Cells(.Rows.Count, "A").End(xlUp).Row
Cells(lr_new_coa, 1) = code
new_coa_detected = 1
Sheets("Month1").Select
existing_code:
'No action for existing code. Check next code
Next l
If new_coa_detected = 1 Then
MsgBox "New COA codes have been detected. A list of these COA codes is available under the 'NewCOAsDetected' tab."
End If
End Sub
【问题讨论】:
-
① 你的最后一个
With ActiveSheet没有End With② 我强烈建议阅读How to avoid using Select in Excel VBA 并将这种技术应用于你的所有代码。 -
另外,如果你一直缩进你的代码,你很可能已经发现了。