嵌套for循环错误VBA

时间:2012-10-19 19:22:47

标签: vba loops for-loop nested

我做了一个嵌套的For循环,如下所示:

For i = 1 To 14
Set curCell_a = Worksheets("Sheet1").Cells(i, 6)    

If curCell_a.Value = 100 Then
Set curCell_b = curCell_a.Offset(3, -1)
cRow = curCell_b.Row     

For j = cRow To 15
Set curCell_c = Worksheets("Sheet1").Cells(cRow, 5)
While curCell_c.Font.Bold = False
MsgBox (curCell_c.Value)
End

Next j    
End If    
Next i

但我一直收到错误Compile error: Next without For

我很确定我按照逻辑顺序放置了Next j, End If, and Next i ...有人可以帮帮我吗?非常感谢你!

2 个答案:

答案 0 :(得分:1)

我认为问题出在End语句中:它应该是Wend(While-End)。

For i = 1 To 14

    Set curCell_a = Worksheets("Sheet1").Cells(i, 6)

    If curCell_a.Value = 100 Then

        Set curCell_b = curCell_a.Offset(3, -1)
        cRow = curCell_b.Row

        For j = cRow To 15
            Set curCell_c = Worksheets("Sheet1").Cells(cRow, 5)
            While curCell_c.Font.Bold = False
                MsgBox (curCell_c.Value)
            Wend
        Next j

    End If

Next i

请参阅http://office.microsoft.com/en-us/excel-help/HV080557576.aspx

答案 1 :(得分:0)

while块需要以Wend结束,而不是End。编译器没有看到该块的结束。