ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   for next question (https://www.excelbanter.com/excel-programming/381545-next-question.html)

Gary Keramidas

for next question
 
i know how to do this a few ways, but have a question.

is something like this possible?

for i = 1 to 3, 5 to 7

just curious because i thought i saw something somewhere, but i could be
mistaken.

--


Gary




JE McGimpsey

for next question
 
There's no such structure in VBA. You'd have to simulate it:

For i = 1 to 7
If i < 4 Then
'Do stuff
End If
Next i

In article ,
"Gary Keramidas" <GKeramidasATmsn.com wrote:

i know how to do this a few ways, but have a question.

is something like this possible?

for i = 1 to 3, 5 to 7

just curious because i thought i saw something somewhere, but i could be
mistaken.


Gary Keramidas

for next question
 
ok, thanks je.

--


Gary


"JE McGimpsey" wrote in message
...
There's no such structure in VBA. You'd have to simulate it:

For i = 1 to 7
If i < 4 Then
'Do stuff
End If
Next i

In article ,
"Gary Keramidas" <GKeramidasATmsn.com wrote:

i know how to do this a few ways, but have a question.

is something like this possible?

for i = 1 to 3, 5 to 7

just curious because i thought i saw something somewhere, but i could be
mistaken.




AA2e72E

for next question
 

Consider:

Sub aa()
For i = 1 To 7
' Within the For..Next loop this is overkill
' BUT the Select ... Case is clearer when handling exceptions
Select Case i
Case 1 To 3 ' or Case Is <= 3
'Code
Case 4
'Code
Case 5 To 7 ' or Case Is =5
'Code
Else
'Code
End Select
Next
End Sub


Leo Heuser

for next question
 
"Gary Keramidas" <GKeramidasATmsn.com skrev i en meddelelse
...
i know how to do this a few ways, but have a question.

is something like this possible?

for i = 1 to 3, 5 to 7

just curious because i thought i saw something somewhere, but i could be
mistaken.

--


Gary




Gary

Maybe this setup for shorter intervals:

Sub test()
'Leo Heuser, 20 Jan, 2007
Dim Counter As Long
Dim NumArray As Variant

NumArray = Array(1, 2, 3, 5, 6, 7)

For Counter = LBound(NumArray) To UBound(NumArray)
Debug.Print NumArray(Counter)
Next Counter

End Sub


Two more options for larger intervals:

Sub test2()
'Leo Heuser, 20 Jan, 2007
Dim Counter As Long
Dim LoopColl As Collection
Dim Num As Variant
Dim NumArray(1 To 3, 1 To 1) As Variant


NumArray(1, 1) = Evaluate("Row(1:3)")
NumArray(2, 1) = Evaluate("Row(5:7)")
NumArray(3, 1) = Evaluate("Row(20:50)")

Set LoopColl = New Collection

For Counter = LBound(NumArray) To UBound(NumArray)
For Each Num In NumArray(Counter, 1)
LoopColl.Add Item:=Num
Next Num
Next Counter

For Counter = 1 To LoopColl.Count
Debug.Print LoopColl(Counter)
Next Counter

End Sub


Sub test3()
'Leo Heuser, 20 Jan, 2007
Dim Counter As Long
Dim Dummy As Variant
Dim LoopColl As Collection
Dim Num As Variant
Dim NumArray As Variant

NumArray = Array(Array(1, 3), Array(5, 7), Array(20, 50))

Set LoopColl = New Collection

For Counter = LBound(NumArray) To UBound(NumArray)
Dummy = Evaluate("Row(" & _
NumArray(Counter)(LBound(NumArray(Counter))) & _
":" & NumArray(Counter)(UBound(NumArray(Counter))) & ")")
For Each Num In Dummy
LoopColl.Add Item:=Num
Next Num
Next Counter

For Counter = 1 To LoopColl.Count
Debug.Print LoopColl(Counter)
Next Counter

End Sub


--
Best regards
Leo Heuser

Followup to newsgroup only please.




Gary Keramidas

for next question
 
thanks for the help. i've already done it with a few arrays. i just thought i
saw somewhere that somebody had done something like i posted. so, i guess i was
mistaken.

--


Gary


"Leo Heuser" wrote in message
...
"Gary Keramidas" <GKeramidasATmsn.com skrev i en meddelelse
...
i know how to do this a few ways, but have a question.

is something like this possible?

for i = 1 to 3, 5 to 7

just curious because i thought i saw something somewhere, but i could be
mistaken.

--


Gary




Gary

Maybe this setup for shorter intervals:

Sub test()
'Leo Heuser, 20 Jan, 2007
Dim Counter As Long
Dim NumArray As Variant

NumArray = Array(1, 2, 3, 5, 6, 7)

For Counter = LBound(NumArray) To UBound(NumArray)
Debug.Print NumArray(Counter)
Next Counter

End Sub


Two more options for larger intervals:

Sub test2()
'Leo Heuser, 20 Jan, 2007
Dim Counter As Long
Dim LoopColl As Collection
Dim Num As Variant
Dim NumArray(1 To 3, 1 To 1) As Variant


NumArray(1, 1) = Evaluate("Row(1:3)")
NumArray(2, 1) = Evaluate("Row(5:7)")
NumArray(3, 1) = Evaluate("Row(20:50)")

Set LoopColl = New Collection

For Counter = LBound(NumArray) To UBound(NumArray)
For Each Num In NumArray(Counter, 1)
LoopColl.Add Item:=Num
Next Num
Next Counter

For Counter = 1 To LoopColl.Count
Debug.Print LoopColl(Counter)
Next Counter

End Sub


Sub test3()
'Leo Heuser, 20 Jan, 2007
Dim Counter As Long
Dim Dummy As Variant
Dim LoopColl As Collection
Dim Num As Variant
Dim NumArray As Variant

NumArray = Array(Array(1, 3), Array(5, 7), Array(20, 50))

Set LoopColl = New Collection

For Counter = LBound(NumArray) To UBound(NumArray)
Dummy = Evaluate("Row(" & _
NumArray(Counter)(LBound(NumArray(Counter))) & _
":" & NumArray(Counter)(UBound(NumArray(Counter))) & ")")
For Each Num In Dummy
LoopColl.Add Item:=Num
Next Num
Next Counter

For Counter = 1 To LoopColl.Count
Debug.Print LoopColl(Counter)
Next Counter

End Sub


--
Best regards
Leo Heuser

Followup to newsgroup only please.






Leo Heuser

for next question
 
"Gary Keramidas" <GKeramidasATmsn.com skrev i en meddelelse
...
thanks for the help. i've already done it with a few arrays. i just
thought i saw somewhere that somebody had done something like i posted.
so, i guess i was mistaken.

--


Gary


You're welcome.

Leo Heuser




All times are GMT +1. The time now is 07:44 PM.

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