View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz[_2_] JLGWhiz[_2_] is offline
external usenet poster
 
Posts: 1,565
Default variable number of For Next loops

One way is to use the If...ElseIf...Then statement:


If x = 2 Then
For i = 1 To 3
Range("A" & i) = i + 2
Next
ElseIf x = 3 Then
For i = 1 To 3
Range("A" & i) = i + 2
Next
For j = 4 T 10
Range("B1" & j) = j + 1
Next
End If

You could also use the Select Case. But the point is that you set a
criteria for VBA to evaluate. If the criteria is met, it does whatever is
behind the door that the criteria opens. If the criteria is not met, it
moves on to the nest executable line of code.



"KAH" wrote in message
...
Is there any way I can have a variable number of nested For ... Next
loops?
In other words, I want x loops. If x=2 there would be 2 loops, if x=3, 3
loops.

This is a follow-up to a question I posted May 29 about all permutations.
The answer I got was helpful and gave me ideas but didn't actually solve
my
problem. I realize what I really need to figure out is the variable number
of
loops.

Thanks for any help.