Results 1 to 8 of 8

Thread: Recursive combinations [please help]

Threaded View

  1. #1

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Recursive combinations [please help]

    How do I make the code below recursive? I'm trying to create all combination of the array. This gives all combinations where the length is exactly 5. I want it to give all combinatoin where the length is 1 to the ubound of array s. So for a length of 3 there would be 3 nested loops, for length of 8 there would be 8 nested loops and so on.

    Ultimately what I want to accomplish is to have an array filled with numbers and find the sum of every possible combination and pick the combination that has a sum that is closet to a target number without going under, but I thought I'd start here first.

    VB Code:
    1. Sub CreateCombos()
    2.  
    3.     Dim s(8)
    4.  
    5.     s(1) = "A"
    6.     s(2) = "B"
    7.     s(3) = "C"
    8.     s(4) = "D"
    9.     s(5) = "E"
    10.     s(6) = "F"
    11.     s(7) = "G"
    12.     s(8) = "H"
    13.  
    14.     lLen = 5
    15.  
    16.     For i1 = 1 To (UBound(s) - lLen) + 1
    17.     For i2 = i1 + 1 To (UBound(s) - lLen) + 2
    18.     For i3 = i2 + 1 To (UBound(s) - lLen) + 3
    19.     For i4 = i3 + 1 To (UBound(s) - lLen) + 4
    20.     For i5 = i4 + 1 To (UBound(s) - lLen) + 5
    21.                    
    22.         Debug.Print s(i1) & s(i2) & s(i3) & s(i4) & s(i5)
    23.    
    24.     Next i5
    25.     Next i4
    26.     Next i3
    27.     Next i2
    28.     Next i1
    29.  
    30. End Sub

    Any help?
    Last edited by WorkHorse; Jul 11th, 2006 at 12:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width