2016-01-29 54 views
0

我想在下面的if條件中繼續for循環,但VBA抱怨編譯錯誤:「Next for For」。
我正在使用「GoTo」,因爲VBA中沒有「continue」關鍵字,例如其他典型的編程語言。VBA 7.0:在for循環內的GoTo編譯器錯誤

任何想法這裏有什麼問題嗎?

For RowIndex = 2 To lastrow 

    If Worksheets("Sheet1").Range("$b$" & RowIndex) = "" Then 
     GoTo EndLoop 

    output_string = setproperty & Worksheets("Sheet1").Range("$b$" & RowIndex) & " [ get_ports " & """" & Worksheets("Sheet1").Range("$g$" & RowIndex) & """" & " ]" 
    Print #1, output_string 

    output_string = setpropertyiostandard & Worksheets("Sheet1").Range("$k$" & RowIndex) & " [ get_ports """ & Worksheets("Sheet1").Range("$g$" & RowIndex) & """" & " ]" 
    Print #1, output_string 


    If Worksheets("Sheet1").Range("$k$" & RowIndex) = "LVCMOS18" Then 
     'Debug.Print "found" & RowIndex 
     output_string = "set_property DRIVE 8 [ get_ports " & Worksheets("Sheet1").Range("$g$" & RowIndex) & "]" 
     Print #1, output_string 
    End If 

EndLoop: 
Next RowIndex 

回答

3

您的第一個If/Then保持打開狀態,需要在for循環中關閉它。

+0

你說得對。謝謝! – electro

0

對於您的情況,您可以將測試從=更改爲<>。另一個訣竅是使用while true loop

For RowIndex = 2 to LastRow 
    While true 
    If Worksheets("Sheet1").Range("$b$" & RowIndex) = "" Then Exit Do 

    Rest of your code here 

    Exit Do '<- Remember to do this or you'll be caught in an infinite loop 
    Loop 
Next