【发布时间】:2019-06-22 01:22:54
【问题描述】:
'I don't know how to write the code, but this is the goal:
'Loop through cells A1 to A10.
'Test if cell A1 meets condition.
'If not, go to A2.
'If yes, do procedure, then go to A2.
Sub TestForIfElse()
Dim Counter As Integer
Dim Counter2 as Integer
Dim i As Integer
For i = 1 To 5
Counter = i
If Counter > 3 Then 'GOTO NEXT i LOOP
Next i 'ERROR MSG NEXT WITHOUT FOR
Else
Counter2 = Counter2 + i
End If
Next i 'GOTO NEXT i LOOP
End Sub
预期:运行下一个循环。
在条件的循环测试中。
如果满足条件,则进入下一个 For 循环。
如果条件不满足,做其他事情,然后去
下一个 for 循环。
代码块上标记的错误消息。
【问题讨论】:
-
去掉第一个
Next i和Else,将条件改为<=3。 -
你不能交错循环
-
尊重块结构。即使它是“基本”... ;)
-
了解其他类型的循环(
While、Until)。了解循环是编程的基础。
标签: vba for-loop if-statement