View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
lifeguardernie lifeguardernie is offline
external usenet poster
 
Posts: 11
Default setting up number combinations

how do i actually apply this code in excel
i'm not used to them

"Joel" wrote:

Below is code I wrote for somebody else earlier this week.

Modify these two line as necessary
'you can make instring as long or short as you want. You can put string in
the array instead of number.
'for 6 players
InStrings = Array(1, 2, 3, 4, 5, 6)
'for 10 players
InStrings = Array(1, 2, 3, 4, 5, 6,7,8,9,10)

modify for size of team or number of teams playing together
ComboLen = 2


Public InStrings
Public combo
Public RowCount
Public ComboLen
Sub combinations()

InStrings = Array(1, 2, 3, 4, 5, 6)
Length = UBound(InStrings) + 1

Level = 1
RowCount = 1
ComboLen = 2
ReDim combo(ComboLen)
Position = 0

Call recursive(Level, Position)
End Sub
Sub recursive(ByVal Level As Integer, ByVal Position As Integer)

Length = UBound(InStrings) + 1

For i = Position To (Length - 1)

'for combinations check if item already entered
found = False
For j = 0 To (Level - 2)
'combo is a count of the combinations,not the actual data
'123
'124
'125
'234
'235
'245
'345
'data is actually in InStrings
If combo(j) = i Then
found = True
Exit For
End If
Next j

If found = False Then
combo(Level - 1) = i
If Level = ComboLen Then
For j = 0 To (ComboLen - 1)
If j = 0 Then
ComboString = InStrings(combo(j))
Else
ComboString = ComboString & "," & InStrings(combo(j))
End If
Next j
Sheets("Sheet2").Range("A" & RowCount) = ComboString
RowCount = RowCount + 1
Else
Call recursive(Level + 1, i)
End If
End If
Next i
End Sub


"lifeguardernie" wrote:

how can i set it up so that i can have the program write out the combinations
between two numbers so that there are never the same number twice
and then add a third number to the equation

i need this for league teams
if i have for example 6 teams and i want them to play every other team
before repeating a team
then i want to add games into this combination so not only dont the play the
same team twice they go through all the games before repeating them again