继续TypeMismatch 2042错误的下一次迭代

我在VBA中寻找Continue语句。

我find了一个VB.NET解决scheme,但Excel“IDE”不能识别它。

http://msdn.microsoft.com/en-us/library/801hyx6f.aspx

如果发生错误,我想跳过迭代。 有没有其他的方法 – 一个更优雅的解决scheme – 但GOTO(这是最糟糕的情况下)或包装整个小组在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 

选项1

如果你想跳过while循环的当前迭代,如果C.Value抛出一个错误,只要把循环C.Value的其余代码放到一个if语句中,然后添加一个fileErrorvariables:

 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 

scheme2

这是完成相同目标的另一个select。 我不认为它比其他选项更干净,但它避免了额外的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循环的下一个迭代处。

您可以使用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 
Interesting Posts