For Next (non consecutive values for i)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When using the For i = 1 to 10
‘Your code’
Next i

Can I define i = 1 to 10 (excluding certain values)?
I want the code to be executed only for i= 1,4,5,6,8,10

Thanks
 
One way:

Dim vArr As Variant
Dim i As Long
Dim j As Long
vArr = Array(1,4,5,6,8,10)
For j = LBound(vArr) to UBound(varr)
i = vArr(j)
'Your code
Next j
 
Sub caroline()
s = Array(1, 4, 5, 6, 8, 10)
For i = LBound(s) To UBound(s)
MsgBox (s(i))
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top