Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
recursive or loopless combinations
Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below? I'm looking for a "loopless" one or a recursive one, sine the number of my sets can vary from 4 to 10 Any help will be appreciated. My five sets are listed below. {1,2,3} {4,5,6,7} (8,9,10} {11,12,13,14} (15,16,17,18} The first combination can be: 1,4,8,11,15 and the last one, 3,7,10,14,18 Regards, |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
recursive or loopless combinations
Try this code
Public InStrings(5) Public Levels Public combo Public RowCount Sub combinations() InStrings(0) = Array(1, 2, 3) InStrings(1) = Array(4, 5, 6, 7) InStrings(2) = Array(8, 9, 10) InStrings(3) = Array(11, 12, 13, 14) InStrings(4) = Array(15, 16, 17, 18) Levels = UBound(InStrings) Level = 1 RowCount = 1 ReDim combo(UBound(InStrings)) Call recursive(Level) End Sub Sub recursive(ByVal Level As Integer) Length = UBound(InStrings(Level - 1)) + 1 For i = 0 To (Length - 1) combo(Level - 1) = InStrings(Level - 1)(i) If Level = Levels Then For j = 0 To (Levels - 1) If j = 0 Then ComboString = combo(j) Else ComboString = ComboString & "," & combo(j) End If Next j Sheets("Sheet2").Range("A" & RowCount) = ComboString RowCount = RowCount + 1 Else Call recursive(Level + 1) End If Next i End Sub "Mazant, K." wrote: Does anybody knows how to generate all the combinations, by selecting one element from each of the sets given below? I'm looking for a "loopless" one or a recursive one, sine the number of my sets can vary from 4 to 10 Any help will be appreciated. My five sets are listed below. {1,2,3} {4,5,6,7} (8,9,10} {11,12,13,14} (15,16,17,18} The first combination can be: 1,4,8,11,15 and the last one, 3,7,10,14,18 Regards, |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
recursive or loopless combinations
Joel wrote:
Try this code Public InStrings(5) Public Levels Public combo Public RowCount Sub combinations() InStrings(0) = Array(1, 2, 3) InStrings(1) = Array(4, 5, 6, 7) InStrings(2) = Array(8, 9, 10) InStrings(3) = Array(11, 12, 13, 14) InStrings(4) = Array(15, 16, 17, 18) Levels = UBound(InStrings) Level = 1 RowCount = 1 ReDim combo(UBound(InStrings)) Call recursive(Level) End Sub Sub recursive(ByVal Level As Integer) Length = UBound(InStrings(Level - 1)) + 1 For i = 0 To (Length - 1) combo(Level - 1) = InStrings(Level - 1)(i) If Level = Levels Then For j = 0 To (Levels - 1) If j = 0 Then ComboString = combo(j) Else ComboString = ComboString & "," & combo(j) End If Next j Sheets("Sheet2").Range("A" & RowCount) = ComboString RowCount = RowCount + 1 Else Call recursive(Level + 1) End If Next i End Sub "Mazant, K." wrote: Does anybody knows how to generate all the combinations, by selecting one element from each of the sets given below? I'm looking for a "loopless" one or a recursive one, sine the number of my sets can vary from 4 to 10 Any help will be appreciated. My five sets are listed below. {1,2,3} {4,5,6,7} (8,9,10} {11,12,13,14} (15,16,17,18} The first combination can be: 1,4,8,11,15 and the last one, 3,7,10,14,18 Regards, Thanks Joel. Regards, |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Is this a recursive problem? | Excel Programming | |||
Recursive Code | Excel Programming | |||
Help with Recursive Call? | Excel Programming | |||
Recursive Functio help | Excel Worksheet Functions | |||
recursive sums | Excel Worksheet Functions |