我在VBA中寻找一个继续声明。
我找到了一个VB.NET解决方案,但Excel" IDE"不认识它。
http://msdn.microsoft.com/en-us/library/801hyx6f.aspx
如果发生错误,我想跳过迭代。有没有其他方法 - 一个更优雅的解决方案 - 但GOTO(imho是最糟糕的情况)或将整个Sub包装在一个If语句中(它也没有帮助可读性)?
代码段
'While Loop....
For Each C In w.Range(w.Cells(1, 1), w.Cells(num_rw, num_col))
If IsError(C.Value) Then:
MsgBox ("File unsuitable!")
'Continue While
End If
If Var1 <> "" Then
If C.Value = Var1 Then: _
Set Var1_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var2 <> "" Then
If C.Value = Var2 Then: _
Set Var2_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var3 <> "" Then
If C.Value = Var3 Then:
Set Var3_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
Next C
If Var1_Range Is Nothing Then: 'Continue While
'WEND
答案 0 :(得分:1)
选项1
如果要跳过While循环的当前迭代,如果C.Value
抛出错误,只需将循环后的其余代码抛出到if语句并添加fileError
变量:< / p>
Dim fileError As Boolean
'While Loop....
fileError = False
For Each C In w.Range(w.Cells(1, 1), w.Cells(num_rw, num_col))
If IsError(C.Value) Then:
MsgBox ("File unsuitable!")
fileError = True
Exit For
'Continue While
Else
If Var1 <> "" Then
If C.Value = Var1 Then: _
Set Var1_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var2 <> "" Then
If C.Value = Var2 Then: _
Set Var2_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var3 <> "" Then
If C.Value = Var3 Then:
Set Var3_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
End If
Next C
If fileError = False Then
If Var1_Range Is Nothing Then: 'Continue While
.
. 'Put the rest of the code in your While loop here
.
End If
End If
'WEND
选项2
这是实现相同目标的另一种选择。我不认为它比其他选项更干净,但它确实避免了额外的If语句和GoTo
函数:
'While Loop....
Do
For Each C In w.Range(w.Cells(1, 1), w.Cells(num_rw, num_col))
If IsError(C.Value) Then:
MsgBox ("File unsuitable!")
Exit Do
'Continue While
Else
If Var1 <> "" Then
If C.Value = Var1 Then: _
Set Var1_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var2 <> "" Then
If C.Value = Var2 Then: _
Set Var2_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var3 <> "" Then
If C.Value = Var3 Then:
Set Var3_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
End If
Next C
If Var1_Range Is Nothing Then: 'Continue While
.
.
.
End If
Exit Do 'Ensures the loop will never execute more than once
Loop
'WEND
然后,您可以将Exit Do
放在想要跳到While循环的下一次迭代的任何地方。
答案 1 :(得分:0)
您可以使用Exit For退出最直接/最新的For。
For Each C In w.Range(w.Cells(1, 1), w.Cells(num_rw, num_col))
If IsError(C.Value) Then:
MsgBox "File unsuitable!"
Exit For
End If
If Var1 <> "" Then
If C.Value = Var1 Then: _
Set Var1_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var2 <> "" Then
If C.Value = Var2 Then: _
Set Var2_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
If Var3 <> "" Then
If C.Value = Var3 Then:
Set Var3_Range = w.Range(w.Cells(C.Row, C.Column), w.Cells(num_rw, C.Column))
End If
Next C
If Var1_Range Is Nothing Then: 'Continue While