PDA

View Full Version : Irregular do loops



baron11
08-27-2008, 03:06 PM
Hi have a peculiar question.

I have a for next statement where the code is executed for i= 1- 125.

For i =1 to 125

code........
code.............

Next

End Sub.

This works fine.

How do I make this work if I want the code to run only for i =3, 10, 57, 69 & 83, 96 ?

How can I make this work ?

Thanks a lot.

Baron11

mikerickson
08-27-2008, 05:12 PM
Dim i as Variant

For each i in Array(3,10,57,69,83,96)
Rem code
Next i

david000
08-28-2008, 01:32 AM
Same as above, but with the For To statement instead of the For Each.
Sub test()
Dim arr As Variant
Dim i As Integer

arr = Array(3, 10, 57, 69, 83, 96)

For i = 0 To UBound(arr) 'zero based


MsgBox arr(i) 'code


Next i

End Sub

Bob Phillips
08-28-2008, 01:49 AM
Option Explicit
Option Base 1

Sub test()
Dim arr As Variant
Dim i As Integer

arr = Array(3, 10, 57, 69, 83, 96)

For i = 0 To UBound(arr) 'zero based

MsgBox arr(i) 'code
Next i

End Sub


and watch it fall over

Charlize
08-28-2008, 07:06 AM
For i = LBound(arr) To UBound(arr)should fix the loop.

Charlize

baron11
08-28-2008, 07:15 AM
Thanks a lot for your input.

Cheers,

Baron11