2012-10-19 132 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

謝謝你們! –