M
Myles
Hi all,
I am re-visiting my initial query (lost in traffic) hoping this time
someone will kindly oblige me. In my desperation to -understand- the
mechanics of this code credited to Tom Ogilvy , I threw in MsgBoxes in
strategic places so to trap and monitor the changing values of the
respective variables and not least the worksheet output. The more
trappings I did, the more I became mystified. Assuming activecell is
A1, and using n = 9 and m = 3, the code populates Column A with all the
84 possible combinations - selecting 3 items at a time from 1,2,3 ...
9.
Sub Combinations()
Dim n As Integer, m As Integer
numcomb = 0
'n = InputBox("Number of items?", , 10)
'm = InputBox("Taken how many at a time?", , 3)
n=9
m=3
com n, m, 1, "'"
End Sub
'Generate combinations of integers k..n taken m at a time, recursively
Sub com(ByVal n%, ByVal m%, ByVal k%, ByVal s as String)
If m > n - k + 1 Then Exit Sub
If m = 0 Then
ActiveCell = s
ActiveCell.Offset(1, 0).Select
MSGBOX M & \" \" & K & \" \" & S
Exit Sub
End If
com n, m - 1, k + 1, s & k & " "
MSGBOX M & \" \" & K & \" \" & S
com n, m, k + 1, s
MSGBOX M & \" \" & K & \" \" & S
End Sub
Myles
I am re-visiting my initial query (lost in traffic) hoping this time
someone will kindly oblige me. In my desperation to -understand- the
mechanics of this code credited to Tom Ogilvy , I threw in MsgBoxes in
strategic places so to trap and monitor the changing values of the
respective variables and not least the worksheet output. The more
trappings I did, the more I became mystified. Assuming activecell is
A1, and using n = 9 and m = 3, the code populates Column A with all the
84 possible combinations - selecting 3 items at a time from 1,2,3 ...
9.
Sub Combinations()
Dim n As Integer, m As Integer
numcomb = 0
'n = InputBox("Number of items?", , 10)
'm = InputBox("Taken how many at a time?", , 3)
n=9
m=3
com n, m, 1, "'"
End Sub
'Generate combinations of integers k..n taken m at a time, recursively
Sub com(ByVal n%, ByVal m%, ByVal k%, ByVal s as String)
If m > n - k + 1 Then Exit Sub
If m = 0 Then
ActiveCell = s
ActiveCell.Offset(1, 0).Select
MSGBOX M & \" \" & K & \" \" & S
Exit Sub
End If
com n, m - 1, k + 1, s & k & " "
MSGBOX M & \" \" & K & \" \" & S
com n, m, k + 1, s
MSGBOX M & \" \" & K & \" \" & S
End Sub
Myles