ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For.. Next with exception (https://www.excelbanter.com/excel-programming/439435-next-exception.html)

Caroline

For.. Next with exception
 
hello,
Any body would know how to do this?

For i=1 to 123
blablabla
next

But without doing certain values of i (lets say 50,60,70)

I could write
For i = 1 to 49
blablabla
next
for i=51 to 59
blablabla
next

but is there a more elegant way?
thanks
--
caroline

Ryan H

For.. Next with exception
 
You could do it two ways.

1.) You could use an If...Then statement inside your loop.

Dim i As Long

For i = 1 To 123
If i = 50 Or i = 60 Or i = 70 Then
' do something
End If
Next i

2.) You could list the i's you want to loop through.

Dim MyArray As Variant

MyArray = Split(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

For i = LBound(MyArray) To UBound(MyArray)
' do something
Next i


Hope this helps! If so, let me know, click "YES" below.
--
Cheers,
Ryan


"caroline" wrote:

hello,
Any body would know how to do this?

For i=1 to 123
blablabla
next

But without doing certain values of i (lets say 50,60,70)

I could write
For i = 1 to 49
blablabla
next
for i=51 to 59
blablabla
next

but is there a more elegant way?
thanks
--
caroline


jamesdbrown1979

For.. Next with exception
 
Public Sub OmitLoopIncrements()

Dim Index As Long

For Index = 1 To 123
Select Case Index
Case 50, 60, 70
Case Else
'your code
End Select
Next Index

End Sub


All times are GMT +1. The time now is 03:01 AM.

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