Hi,
Can someone help me edit this code to list all permutation of n distinct object of size r without repetition. For example, if i have three letters, {A,B,C} to form arrangement of size two, i will get: {A,B}, {A,C}, {B,A}, {B,C}, {C,A} and {C,B}. {A,A}, {B,B} and {C,C} are NOT included..
The vba code is pasted below
Thank you.
Sub Permutations()
Dim rRng As Range, p As Integer
Dim vElements, lRow As Long, vresult As Variant
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Columns("C").Resize(, p + 1).Clear
Call PermutationsNP(vElements, p, vresult, lRow, 1, 1)
End Sub
Sub PermutationsNP(vElements As Variant, p As Integer, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
Dim i As Integer
For i = 1 To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
lRow = lRow + 1
Range("C" & lRow).Resize(, p) = vresult
Else
Call PermutationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
End If
Next i
End Sub
Can someone help me edit this code to list all permutation of n distinct object of size r without repetition. For example, if i have three letters, {A,B,C} to form arrangement of size two, i will get: {A,B}, {A,C}, {B,A}, {B,C}, {C,A} and {C,B}. {A,A}, {B,B} and {C,C} are NOT included..
The vba code is pasted below
Thank you.
Sub Permutations()
Dim rRng As Range, p As Integer
Dim vElements, lRow As Long, vresult As Variant
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Columns("C").Resize(, p + 1).Clear
Call PermutationsNP(vElements, p, vresult, lRow, 1, 1)
End Sub
Sub PermutationsNP(vElements As Variant, p As Integer, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
Dim i As Integer
For i = 1 To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
lRow = lRow + 1
Range("C" & lRow).Resize(, p) = vresult
Else
Call PermutationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
End If
Next i
End Sub