PDA

View Full Version : [SOLVED] For Loop with multiple conditions.



Tdude
08-28-2014, 10:16 AM
.

Tdude
08-28-2014, 10:28 AM
Dear All,

a quick one....

I am dealing with a nested for/next loop but I actually need both i and j to change at the same time and not one at the time.

something like "For i = 1 To 30 and j=0 to 87" or "For i = 1 To 30, j=0 to 87"

I'm sure it is easier than it looks but just can't find a way.

Do you have any suggestions, please?

Thanks in advance

the sample code is the following





Sub Trial()


For i = 1 To 30

For j = 0 To 87 Step 3

With Range("A1")

Set myRange = Range(.Offset(1, i + j), .Offset(1, i + j).End(xlDown))

End With


Next j

Next i


End Sub

SamT
08-28-2014, 10:42 AM
J = -3
For 1 = 1 to 30
J = J + 3
blah blah
Next I

Bob Phillips
08-28-2014, 03:14 PM
J = -3
For 1 = 1 to 30
J = J + 3
blah blah
Next I

Do you mean


For i = 1 To 30
j = i * 3 - 3
'etc
Next i

SamT
08-28-2014, 04:48 PM
mathematically identical results

But mine's faster :LOL

Bob Phillips
08-29-2014, 05:08 AM
Sorry, it was the line


For 1 = 1 To 30

which I was suggesting that you really meant


For i = 1 To 30

in your code that I was remarking upon, I just changed the way j was set as it seemed more intuitive (at least to me it did).

Tdude
08-29-2014, 05:56 AM
Thank you both for your help !!

SamT
08-29-2014, 07:27 AM
Yes, I meant i =1 to 30

My finger always reaches first for the 1 when I use lower case I :dunno

The way Tdude worded his Q made that J = J +3 jump into my (weak, medically abused, and pain filled) head. After I had it in print I looked askance at it for a while thinking.

I came to the conclusion that is is actually a bit faster than using a multiplier on the outer loop counter.

I'm so happy now! I saved 30 seconds/petaflop!