2016-01-29 76 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