2013-12-19 94 views
5

我想創建一個簡單的條件循環,如果條件爲true,它將轉到下一個迭代。我到目前爲止的代碼是:跳轉到循環vba中的下一個迭代

For i = 2 To 24 
Level = Cells(i, 4) 
Return = Cells(i, 5) 

If Return = 0 And Level = 0 Then 
'Go to the next iteration 
Else 
End If 

我試圖轉到NextIteration,但這又發表了錯誤「沒有定義標籤」。這可能有一個非常簡單的解決方案,但非常感謝。謝謝

+0

什麼級別什麼回報,那麼你的電子表格看起來像 – 2013-12-19 12:07:17

+1

倒置的條款和下降throught? - 或http://stackoverflow.com/questions/5895908/continue-for-loop –

+0

在VbScript中沒有'Continue'。你將不得不圍繞「If-Else」來跳過任何迭代。通過@AlexK發佈的鏈接。它有一些很好的建議。 –

回答

5

一旦符合標準,什麼都不做,否則做你需要的處理,並且For循環將轉到下一個項目。

For i = 2 To 24 
    Level = Cells(i, 4) 
    Return = Cells(i, 5) 

    If Return = 0 And Level = 0 Then 
     'Do nothing 
    Else 
     'Do something 
    End If 
Next i 

或更改條款,因此只有滿足條件的進程:

For i = 2 To 24 
    Level = Cells(i, 4) 
    Return = Cells(i, 5) 

    If Return <> 0 Or Level <> 0 Then 
     'Do something 
    End If 
Next i 
+0

不應該是如果返回<> 0或水平<> 0然後,所以它是相反的,如果返回= 0並且Level = 0那麼? – brWHigino

+0

第二個似乎工作,但我認爲我需要一個或而不是一個。我想也需要一個Else。 –

1

你想要什麼遵循模式

If (your condition) Then 
    'do your job 
End If 

在這種情況下,你的條件是Not(Return = 0 And Level = 0),所以你會使用

If (Not(Return = 0 And Level = 0)) Then 
    'do your job 
End If 

PS:條件相當於(Return <> 0 Or Level <> 0)

16
For i = 2 To 24 
    Level = Cells(i, 4) 
    Return = Cells(i, 5) 

    If Return = 0 And Level = 0 Then GoTo NextIteration 
    'Go to the next iteration 
    Else 
    End If 
    ' This is how you make a line label in VBA - Do not use keyword or 
    ' integer and end it in colon 
    NextIteration: 
Next 
0
For i = 2 To 24 
Level = Cells(i, 4) 
Return = Cells(i, 5) 

If Return = 0 And Level = 0 Then 
i = 24 
Else 
End If 

只是使它跳轉到循環的結束?

1

我使用goto

For x= 1 to 20 

     If something then goto continue 

     skip this code 

    Continue: 

    Next x 
相關問題