Sub test()
Dim resultArray As Variant
Dim tempArray() As Long
Dim Size As Long
Dim i As Long, lastFixed As Long, pointer As Long
Dim lowTrans As Long, highTrans As Long
Size = Application.InputBox("How many?", Default:=5, Type:=1)
If Size < 1 Then Exit Sub
ReDim resultArray(1 To fact(Size))
ReDim tempArray(1 To Size)
For i = 1 To Size
tempArray(i) = i
Next i
resultArray(1) = tempArray
pointer = 1
lastFixed = 1
highTrans = 2
Do Until Size < highTrans
lowTrans = 1
Do Until lowTrans = highTrans
For i = 1 To lastFixed
tempArray = resultArray(i)
tempArray(lowTrans) = resultArray(i)(highTrans)
tempArray(highTrans) = resultArray(i)(lowTrans)
pointer = pointer + 1
resultArray(pointer) = tempArray
Next i
lowTrans = lowTrans + 1
Loop
lastFixed = pointer
highTrans = highTrans + 1
Loop
With Sheet1
.Cells.ClearContents
For i = 1 To fact(Size)
.Range("A1").Offset(i, 0).Resize(1, Size) = resultArray(i)
Next i
End With
End Sub
Function fact(a)
If a <= 1 Then
fact = 1
Else
fact = a * fact(a - 1)
End If
End Function
What are you doing with this list?
00000 is not a "no permutation"
What are you doing with this list?
Function UniquePermutes(BaseArray As Variant)
Dim Permuting As Variant
Dim i As Long, j As Long, Pointer As Long
Dim resultArray() As Variant
Dim scoreKeeper() As String
Dim tempArray As Variant
If TypeName(BaseArray) = "Range" Then
If BaseArray.Cells.Count = 1 Then
BaseArray = Array(BaseArray.Value)
ElseIf BaseArray.Rows.Count = 1 Then
BaseArray = Application.Transpose(Application.Transpose(BaseArray.Value))
ElseIf BaseArray.Columns.Count = 1 Then
BaseArray = Application.Transpose(BaseArray.Value)
Else
UniquePermutes = CVErr(xlErrValue)
End If
ElseIf Not (BaseArray) Like "*()" Then
UniquePermutes = CVErr(xlErrNum)
End If
Permuting = Permutations(UBound(BaseArray))
ReDim resultArray(1 To UBound(Permuting))
ReDim scoreKeeper(1 To UBound(Permuting))
tempArray = BaseArray
For i = 1 To UBound(Permuting)
For j = 1 To UBound(tempArray)
tempArray(j) = BaseArray(Permuting(i)(j))
Next j
If IsError(Application.Match(Join(tempArray), scoreKeeper, 0)) Then
Pointer = Pointer + 1
scoreKeeper(Pointer) = Join(tempArray)
resultArray(Pointer) = tempArray
End If
Next i
ReDim Preserve resultArray(1 To Pointer)
UniquePermutes = resultArray
End Function
Function Permutations(Size As Long) As Variant
Dim resultArray As Variant
Dim tempArray() As Long
Dim i As Long, lastFixed As Long, Pointer As Long
Dim lowTrans As Long, highTrans As Long
If Size < 1 Then Exit Function
ReDim resultArray(1 To fact(Size))
ReDim tempArray(1 To Size)
For i = 1 To Size
tempArray(i) = i
Next i
resultArray(1) = tempArray
Pointer = 1
lastFixed = 1
highTrans = 2
Do Until Size < highTrans
lowTrans = 1
Do Until lowTrans = highTrans
For i = 1 To lastFixed
tempArray = resultArray(i)
tempArray(lowTrans) = resultArray(i)(highTrans)
tempArray(highTrans) = resultArray(i)(lowTrans)
Pointer = Pointer + 1
resultArray(Pointer) = tempArray
Next i
lowTrans = lowTrans + 1
Loop
lastFixed = Pointer
highTrans = highTrans + 1
Loop
Permutations = resultArray
End Function
Function fact(a)
If a <= 1 Then
fact = 1
Else
fact = a * fact(a - 1)
End If
End Function