Option Explicit
Sub test2()
Dim ar, cr, dr, er, dic As Object
Dim i&, j&, r&, m&, n&
ReDim cr(1 To 6, 1 To 6)
For j = 1 To 6
For i = 1 To 6
cr(i, j) = i
Next i
Next j
m = UBound(cr): n = UBound(cr, 2)
ReDim dr(1 To n)
ReDim er(1 To m ^ n)
Call cartesianProductDG1(cr, dr, er) ', r)
cr = Range("A1", Cells(Rows.Count, "A").End(xlUp)).Value
ReDim dr(1 To UBound(er), 0)
Set dic = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(cr)
dic(CStr(cr(i, 1))) = Empty
Next i
For i = 1 To UBound(er)
If Not dic.exists(er(i)) Then
r = r + 1
dr(r, 0) = er(i)
End If
Next i
Columns("E").Clear
[E1].Resize(r) = dr
End Sub
Function cartesianProductDG1(ByVal ar, ByVal br, ByRef vResult, _
Optional ByRef iGroup&, Optional ByVal n& = 1)
Dim i&, j&
For i = 1 To UBound(ar)
br(n) = ar(i, n)
If n = UBound(ar, 2) Then
iGroup = iGroup + 1
vResult(iGroup) = Join(br, "")
Else
cartesianProductDG1 ar, br, vResult, iGroup, n + 1
End If
Next
End Function
|