Excel VBA – 为特定值的循环
我试图创build一个For循环只select一些值,但不能解决语法,或者甚至可能?
我希望它是像
Dim i As Integer For i = 1,3,8,15 Then Do something Next i
有任何想法吗?
尝试
Sub Demo() Dim indexArr As Variant Dim i As Long indexArr = Array(1, 3, 8, 15) For i = LBound(indexArr) To UBound(indexArr) Debug.Print indexArr(i) Next i End Sub
你不能。
如果有一个模式,你可以使用Step
For i = 1 to 15 Step 2
哪个会做1,3,5,7,...
在这种情况下,因为没有模式,您将需要添加一个If
或Select Case
:
Dim i As Integer For i = 1 to 15 Then Select Case i Case 1,3,8,15 'Do Something End Select Next i
你可以使用if语句:
Dim i As Integer For i = 1 To 15 Then If i = 1 or i = 3 or i = 8 or i = 15 Then 'Do something End If Next i
希望这个帮助。