Option Explicit
Sub PermutationsN_1ToP_R()
Dim vElements As Variant, vresult As Variant
Dim lRow As Long, i As Long, row_count As Integer
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ActiveSheet
Set ws2 = Sheets.Add
For row_count = 1 To ws1.Range("B" & Rows.count).End(xlUp).Row
If Not ws1.Range("B" & row_count) = "" Then
vElements = Split(ws1.Range("B" & row_count))
i = UBound(vElements)
ReDim vresult(0 To UBound(vElements))
Call PermutationsNPR(ws1, ws2, vElements, UBound(vElements), vresult, lRow, 0, row_count)
End If
Next
End Sub
Sub PermutationsNPR(ws1 As Worksheet, ws2 As Worksheet, vElements As Variant, p As Long, vresult As Variant, lRow As Long, iIndex As Integer, row_count As Integer)
Dim i As Long, count, found_it As Boolean
lRow = ws2.Range("C" & Rows.count).End(xlUp).Row
For i = 0 To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
lRow = lRow + 1
ws2.Range("D" & lRow).Resize(, p + 1) = vresult
found_it = False
For count = 4 To ws2.Cells(lRow, Columns.count).End(xlToLeft).Column
If WorksheetFunction.CountIf(ws2.Range(Cells(lRow, 4), ws2.Cells(lRow, ws2.Cells(lRow, Columns.count).End(xlToLeft).Column)), ws2.Cells(lRow, count)) > 1 Then
found_it = True
End If
Next
If found_it = True Then
ws2.Range(ws2.Cells(lRow, 4), ws2.Cells(lRow, ws2.Cells(lRow, Columns.count).End(xlToLeft).Column)).ClearContents
lRow = lRow - 1
Else
ws2.Range("A" & lRow) = ws1.Range("A" & row_count)
ws2.Range("B" & lRow) = ws1.Range("B" & row_count)
ws2.Range("C" & lRow) = vresult(0)
For count = 1 To UBound(vElements)
ws2.Range("C" & lRow) = ws2.Range("C" & lRow) & " " & vresult(count)
Next
Range(Cells(lRow, 4), Cells(lRow, Cells(lRow, Columns.count).End(xlToLeft).Column)).ClearContents
End If
SKIP_THIS_ONE:
Else
Call PermutationsNPR(ws1, ws2, vElements, p, vresult, lRow, iIndex + 1, row_count)
End If
Next i
End Sub
Bookmarks