Recursive function

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
Hello,

I need a function that gets a variable number (byte its Ok), and do the following:

if the variable is 1 it will do the resulte of this function:
VBA Code:
For Each v In Array(1, 2, 3, 4)
   Debug.Print v
Next

if the the variable is 2 it will do the resulte of this function:
VBA Code:
    For Each v In Array(1, 2, 3, 4)
        For Each vv In Array(1, 2, 3, 4)
            Debug.Print v, vv
        Next
    Next

if the the variable is 3 it will do the resulte of this function:
VBA Code:
For Each v In Array(1, 2, 3, 4)
    For Each vv In Array(1, 2, 3, 4)
        For Each vvv In Array(1, 2, 3, 4)
            Debug.Print v, vv, vvv
        Next
    Next
Next

and so on........

of course it should be dynamic, dont use "select case" (for the variable) or somthing like that.

I think it needs to be a recursive function, but I'm not sure.
 
Glad we could help. Thanks for the update. You definitely sparked an interesting discussion.

For those who don't like GOTO, here's a variation without it:

VBA Code:
Sub VariableCounter()
Dim NumIx As Long, Indexes() As Long, Maxes() As Long, i As Long, AllDone As Boolean

    NumIx = 3                       ' How many levels do you want?
    ReDim Indexes(1 To NumIx)       ' set the array to hold the counters
    ReDim Maxes(1 To NumIx)         ' set the array to hold the max values for each level
    For i = 1 To NumIx              ' for each level:
        Maxes(i) = 4                ' set the max
        Indexes(i) = 1              ' set the starting value
    Next i
            
    AllDone = False
    While Not AllDone
        For i = 1 To NumIx              ' This part just prints out each index, I assume
            Debug.Print Indexes(i),     ' you actually want to do something else with
        Next i                          ' the indices.
        Debug.Print
    
        For i = 1 To NumIx              ' This part increments the indices.  Think odometer.
            Indexes(i) = Indexes(i) + 1 ' Add 1 to the leftmost index.  If it is in the right
            If Indexes(i) <= Maxes(i) Then Exit For    ' range, go and process it.
            Indexes(i) = 1              ' If it exceeds the range, set it back to the starting
        Next i                          ' point, and increment the next one.  If you get all
        If i > NumIx Then AllDone = True  ' the way through the loop, you've done them all.
    Wend
    
End Sub
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top