recursive loop with unknow number of levels

bolo

Active Member
Joined
Mar 23, 2002
Messages
423
HI all you experts, Need a bit of a hand defining a macro structure.

What i am trying to come up with is a macro that will do the following.

Imagine i have the following data

3,2,3

What i am looking to do, is to decrease the right most number down to 1, then decrease the the next left number by 1, reset the right most number and decrease again. I need this process to reduce all the numbers to 1.eg

3,2,2
3,2,1
3,1,3
3,1,2
3,1,1
2,2,3
2,2,2
2,2,1
2,1,3
etc...

However the problem is, that the number of levels (3 in this example), can vary. Obviously if it was just 3 levels i can just use nested if statements, but as i do not know how many levels it is i can't use nested ifs.

The background to this is i am doing a spiderweb type nearest neighbour search. so if the first neighbour is good, i end the search. If the first neighbour is not good, i need to branch out to the next ones and so on. Therefore i do not know at the start how many loops i need to compute. SO i need to come up with a recursive type loop to control this. Not 100% sure how to do this in a vba macro so any pointers gratefully received.

Regards

Bolo
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
A gentle bump... I have spent the day do my head in with numerous do loops.... Just can't get my head around it!!!! Can anyone get me started?
 
Upvote 0
Hi Bolo

Not sure I understood completely, but try running this code:

Code:
Sub Test()
Dim j As Long, lRow As Long
Dim bStop As Boolean
Dim vArr As Variant, vArr1 As Variant
 
' vArr = Array(3, 2, 3)
vArr = Array(2, 3, 2, 4)
vArr1 = vArr
 
Do
    lRow = lRow + 1
    Range("A" & lRow).Resize(1, UBound(vArr) - LBound(vArr) + 1).Value = vArr1
    For j = UBound(vArr) To LBound(vArr) Step -1
        If vArr1(j) > 1 Then vArr1(j) = vArr1(j) - 1 Else vArr1(j) = vArr(j)
        If vArr1(j) <> vArr(j) Then Exit For
        If j = LBound(vArr) Then bStop = True
    Next j
Loop While Not bStop
    
End Sub

This code writes the result starting in A1, one value per column.
 
Upvote 0
You sir are a gent.

I will try modifying this to see if i can get my procedure to work

Thanks

Bolo

Hi Bolo

Not sure I understood completely, but try running this code:

Code:
Sub Test()
Dim j As Long, lRow As Long
Dim bStop As Boolean
Dim vArr As Variant, vArr1 As Variant
 
' vArr = Array(3, 2, 3)
vArr = Array(2, 3, 2, 4)
vArr1 = vArr
 
Do
    lRow = lRow + 1
    Range("A" & lRow).Resize(1, UBound(vArr) - LBound(vArr) + 1).Value = vArr1
    For j = UBound(vArr) To LBound(vArr) Step -1
        If vArr1(j) > 1 Then vArr1(j) = vArr1(j) - 1 Else vArr1(j) = vArr(j)
        If vArr1(j) <> vArr(j) Then Exit For
        If j = LBound(vArr) Then bStop = True
    Next j
Loop While Not bStop
    
End Sub

This code writes the result starting in A1, one value per column.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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