View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
joel joel is offline
external usenet poster
 
Posts: 9,101
Default How to create a list of permutations and combinations?

Try this code

Public InStrings
Public Combo() As Variant
Public RowCount
Public ComboLen
Sub combinations()


InStrings = Array("ACERQL", "BDKSPN")

ComboLen = Len(InStrings(0))
ReDim Combo(0 To (ComboLen - 1))

Level = 1
RowCount = 1
Call Recursive(Level)

End Sub
Sub Recursive(ByVal Level As Integer)


For i = 0 To UBound(InStrings)

Combo(Level - 1) = Mid(InStrings(i), Level, 1)
If Level = ComboLen Then
For ColCount = 1 To ComboLen
Sheets("Sheet1").Cells(RowCount, ColCount) = Combo(ColCount - 1)
Next ColCount
RowCount = RowCount + 1
Else
Call Recursive(Level + 1)
End If

Next i
End Sub




"Malcolm" wrote:

I have a matrix, 6 columns x 2 rows, which contains letter codes. Example:

A C E R Q L
B D K S P N

Each column is a choice; that is, either A or B; either C or D, etc.

I need to create a list of all possible permutations.
There are 64 permutations, e.g.
ACERQL
ACERQN
ACERPL
ACERPN
etc.

The letters in my 12 cells sometimes change based on formulas elsewhere in
the document, so the list needs to change with them.

To make things even more complicated, some columns will sometimes have THREE
choices in them instead of two (again, changing based on formulas), so the
ideal answer would enable me to deal with those cases automatically.