2012-10-19 113 views
0

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

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的逻辑顺序...有人可以帮帮我吗?非常感谢!

回答

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

0

A,而块需要与WEND结束,没有结束。编译器没有看到该块的结尾。

+0

Ooops,Slippery Pete打我吧... –

+0

谢谢你们! –