我只循环通过第一次迭代
我有我的循环麻烦。 出于某种原因,我的循环只经过第一次迭代( i=0
),然后停止。 这是我的代码:
Private Sub DeleteFillerRows() Dim firstRow As Integer Dim lastRow As Integer Dim searchRange As Range Dim i As Integer For i = 0 To i = 2 If i = 0 Then lastRow = 1 End If MsgBox (i) 'used for testing, only displays the first i (0) Set searchRange = Range("A" & CStr(lastRow) & ":A1000") firstRow = searchRange.Find(What:="Name /", LookAt:=xlWhole).Row MsgBox (firstRow) lastRow = searchRange.Find(What:="Region 1", LookAt:=xlWhole).Row - 2 MsgBox (lastRow) Next i End Sub
谁能帮我这个? 我觉得愚蠢的问一个简单的循环,但我不能看到我可能出错的地方。
For
循环的界限如下所示:
For <counter> = <start> To <end>
在你的情况下, <counter>
是variablesi
, <start>
是0
, <end>
是expression式i = 2
。
它只执行一次的原因是因为expression式i = 2
计算结果是一个Boolean
,并且它总是为false,因为循环的初始化器将其设置为0
(这是False
)。
如果你用括号括起来重写你的代码,那么发生的事情就更加清晰了:
For i = 0 To (i = 2)
… …变
For i = 0 To False
…这是…
For i = 0 To 0
正如@ScottCraner在评论中正确指出的那样,只需将该行更正为:
For i = 0 to 2