ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Simple question on For...Next loop (https://www.excelbanter.com/excel-programming/335542-simple-question-next-loop.html)

ALEX

Simple question on For...Next loop
 
Hello.

This should be easy for the pros of the list

I am used to writing For...Next loops as...

For i = 1 to 10

.... and sometimes using the Step 2 to not move up by increments of 1.

How do I use the loop structure for an eclectic bunch of numbers e.g.

1,2, 5,7, 12, 15,20

So for example I tried

For col = 1, 2, 5, 7, 12, 15, 20

....but VBA said 'no'.

I managed to do this by forming an array...but I am sure there is an easier
route.

Regards


Alex

Norman Jones

Simple question on For...Next loop
 
Hi Alex,

Sub Test2()

Dim arr As Variant
Dim i As Long

arr = Array(1, 2, 5, 7, 12, 15, 20)


For i = LBound(arr) To UBound(arr)
'do something. e.g.:
MsgBox arr(i)


Next

End Sub

---
Regards,
Norman



"Alex" wrote in message
...
Hello.

This should be easy for the pros of the list

I am used to writing For...Next loops as...

For i = 1 to 10

... and sometimes using the Step 2 to not move up by increments of 1.

How do I use the loop structure for an eclectic bunch of numbers e.g.

1,2, 5,7, 12, 15,20

So for example I tried

For col = 1, 2, 5, 7, 12, 15, 20

...but VBA said 'no'.

I managed to do this by forming an array...but I am sure there is an
easier
route.

Regards


Alex





All times are GMT +1. The time now is 08:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com