Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KAH KAH is offline
external usenet poster
 
Posts: 6
Default variable number of For Next loops

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default variable number of For Next loops

you need to read up on RECURSION
it sounds like what you really need is a recursive function ... ie one that
calls itself.
an example would be when finding all the files in a folder ... the function
created to find files would call itself if for any subfolders.
Another example would be for factorial numbers eg 5! is the same as 5x4!
which is 5x5x3! ...



"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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default variable number of For Next loops

You can't create nested loops "on the fly", but maybe a solution like this
would work for you. Set up all the nested loops you will possibly need and
check whether that level should be run or not in its first statement and, if
not, exit the loop before it does anything (this will short circuit and
further nested loops right then and there). Here is an example structure for
you to consider (I set it up for 4 nested loops, but the concept can be
carried on as needed)...

NumberOfLoops = 2
For A = # To #
'
' Code for the "A" loop
'
For B = # To #
If NumberOfLoops = 1 Then Exit For
'
' Code for the "B" loop
'
For C = # To #
If NumberOfLoops = 2 Then Exit For
'
' Code for the "C" loop
'
For D = # To #
If NumberOfLoops = 3 Then Exit For
'
' Code for the "D" loop
'
Next
Next
Next
Next

--
Rick (MVP - Excel)


"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.


  #4   Report Post  
Posted to microsoft.public.excel.programming
KAH KAH is offline
external usenet poster
 
Posts: 6
Default variable number of For Next loops

Thanks to everyone for their rapid responses. This idea of exiting loops
works best for my program and is simple. Thanks!

"Rick Rothstein" wrote:

You can't create nested loops "on the fly", but maybe a solution like this
would work for you. Set up all the nested loops you will possibly need and
check whether that level should be run or not in its first statement and, if
not, exit the loop before it does anything (this will short circuit and
further nested loops right then and there). Here is an example structure for
you to consider (I set it up for 4 nested loops, but the concept can be
carried on as needed)...

NumberOfLoops = 2
For A = # To #
'
' Code for the "A" loop
'
For B = # To #
If NumberOfLoops = 1 Then Exit For
'
' Code for the "B" loop
'
For C = # To #
If NumberOfLoops = 2 Then Exit For
'
' Code for the "C" loop
'
For D = # To #
If NumberOfLoops = 3 Then Exit For
'
' Code for the "D" loop
'
Next
Next
Next
Next

--
Rick (MVP - Excel)


"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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
variable number of For Next loops Dave Peterson Excel Programming 0 June 10th 09 03:18 PM
variable number of For Next loops JLGWhiz[_2_] Excel Programming 0 June 10th 09 03:13 PM
Variable Step rates in loops gtslabs Excel Programming 2 February 16th 09 03:19 AM
Error Number: 91 object variable or With block Variable not set Hifni Excel Programming 1 January 9th 08 11:56 AM
variable string and loops Mourinho Excel Programming 2 October 27th 04 09:15 PM


All times are GMT +1. The time now is 10:04 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"