Text Combinations
I always use recursive algorithms to perform this task. Use code below.
Changing Instring to any legth or any strings of character will produce
different combinations
Public InStrings
Public combo
Public RowCount
Sub combinations()
InStrings = Array("A", "B", "C")
Length = UBound(InStrings) + 1
ReDim combo(Length)
Level = 1
RowCount = 1
Call recursive(Level)
End Sub
Sub recursive(ByVal Level As Integer)
Length = UBound(InStrings) + 1
For i = 0 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
'132
'213
'231
'312
'321
'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 = Length Then
ComboString = ""
For j = 0 To (Length - 1)
ComboString = ComboString & InStrings(combo(j))
Next j
Sheets("Sheet2").Range("A" & RowCount) = ComboString
RowCount = RowCount + 1
Else
Call recursive(Level + 1)
End If
End If
Next i
End Sub
"Les" wrote:
Hello
I have the letters A - J (10 in total).
There is 1 letter in each cell from A1 to A10 or A1 to J1 if you prefer.
I'm trying to produce a list of combinations, not permutations, for however
many characters I use and what the remaining combination of characters are.
e.g. I want to use 5 characters a - e, what are all possible combinations of
those 5 charcaters and what are possible combinations of the remaining
characters.
another e.g. I want to use 3 characters c - e, what are the combinations of
those 3 and then the remaining 7
Thanks in advance
|