View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
JLatham JLatham is offline
external usenet poster
 
Posts: 3,365
Default change increasment in a "for" loop

It's generally considered bad form to mess with loop control values, but it
could be done, cautiously like this. and in this case the very first loop of
j causes nothing to happen with 3, 4 and 5 - because it immediately jumps to
6. Try it and look at the output and you could play with things a bit to see
how to adjust counters and such to get the results you want. This would put
things in rows 7,8,9 13,14,15 19,20,21 and 25,26,27 (in columns B and C).

Sub TestLoopAltering()
Dim i As Integer
Dim j As Integer

For i = 1 To 2
For j = 3 To 27
If (j Mod 3) = 0 Then
j = j + 3
If j 27 Then
Exit For
End If
End If
Range("A1").Offset(j, i) = "Here I am"
Next
Next

End Sub

"Jared" wrote:

I do have another question. Is there an option to increase the number by 3
but only on the 3rd fold? running 4 numbers and everytime it reaches a number
which divides into a whole 3 climb by 3.

explenation: 3,4,5,6,9,10,11,12,15,16,17,18,21,22,23 etc..


Thanks, Jared

"JLatham" wrote:

use the Step option of the For statement:

For i = 3 To 27 Step 3

negative numbers are also allowed as
For i = 27 to 3 Step -3

"Jared" wrote:

I have a loop function
For i = 3 To 27
If isempty(i,j) then next i


How do i make i increase by 3?