Hi Gordon
This is a solution.
In this case each one of the distinguishable elements will appear repeated a specific number of times.
In this example that I post, we have permutations with repetition with 2 "a"'s, 1 "b" and 2 "c"'s
The input is a table where in the first column you list the elements and in the second column you list the number of times that they repeat
Please test.
Code:
Option Explicit
' PGC - AUG 2016
' Permutation with repetition and with restrictions.
' A set of distinguishable objects is given, as well and the number of times each one is repeated
' Ex. Permutations with repetition with 2 "a"'s, 1 "b" and 2 "c"'s
' Input in a nx2 table, first columns the elements and second column how many times they repeat
Sub PermMultRep()
Dim vIn As Variant, vPerms As Variant, vPerm As Variant
Dim lRow As Long
vIn = Range("A2", Range("A2").End(xlDown)).Resize(, 2).Value ' table of elements and number of times they repeat
ReDim vPerm(1 To Application.Sum(Application.Index(vIn, 0, 2))) ' array for the current permutation
ReDim vPerms(1 To Application.MultiNomial(Application.Index(vIn, 0, 2)), 1 To UBound(vPerm)) 'array to store all permutations
PermMultRep1 vIn, vPerm, vPerms, 1, lRow ' calculate all the permutations into the vPerms array
Columns("D").Resize(, UBound(vPerm) + 1).Clear ' clears columns for the output
Range("D2").Resize(UBound(vPerms, 1), UBound(vPerms, 2)).Value = vPerms ' writes the output in E2, down and across
End Sub
Sub PermMultRep1(ByVal vIn As Variant, vPerm As Variant, vPerms As Variant, ByVal lInd As Long, lRow As Long)
Dim j As Long, lCol As Long
Dim v1 As Variant
For j = LBound(vIn, 1) To UBound(vIn, 1)
If vIn(j, 2) > 0 Then
vPerm(lInd) = vIn(j, 1)
If lInd = UBound(vPerm) Then
lRow = lRow + 1
For lCol = 1 To UBound(vPerm)
vPerms(lRow, lCol) = vPerm(lCol)
Next lCol
Else
v1 = vIn
v1(j, 2) = v1(j, 2) - 1
PermMultRep1 v1, vPerm, vPerms, lInd + 1, lRow
End If
End If
Next j
End Sub
Input is in A2, down and across.
[TABLE="width: 2"]
<TBODY>[TR]
[TH][/TH]
[TH="align: center"]A
[/TH]
[TH="align: center"]B
[/TH]
[TH="width: 30, align: center"]C
[/TH]
[TH="width: 30, align: center"]D
[/TH]
[TH="width: 30, align: center"]E
[/TH]
[TH="width: 30, align: center"]F
[/TH]
[TH="width: 30, align: center"]G
[/TH]
[TH="width: 30, align: center"]H
[/TH]
[TH="width: 30, align: center"]I
[/TH]
[/TR]
[TR]
[TD="align: center"]
1
[/TD]
[TD="align: left"]Element
[/TD]
[TD="align: left"]Quantity
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
2
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"]2
[/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
3
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"]1
[/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
4
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"]2
[/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
5
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
6
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
7
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
8
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
9
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
10
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
11
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
12
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
13
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
14
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
15
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
16
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
17
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
18
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
19
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
20
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
21
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
22
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
23
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
24
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
25
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
26
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
27
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
28
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
29
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
30
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
31
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]c
[/TD]
[TD="align: left"]b
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: left"]a
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]
32
[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="colspan: 10"][Book1]Sheet4
[/TD]
[/TR]
</TBODY>[/TABLE]