Below is my code. I would like to achieve the same result by recursive method because the number of nested loops is varying from 2 to max 8.
Sub permutation()
c1 = Array(1, 2)
c2 = Array(3, 4)
c3 = Array(5, 6)
c4 = Array(7, 8)
c5 = Array(9, 10)
c6 = Array(11, 12)
c7 = Array(13, 14)
c8 = Array(15, 16)
With Sheets("Criteria")
.Cells.Clear
n = 1
For a = LBound(c1) To UBound(c1)
For b = LBound(c2) To UBound(c2)
For c = LBound(c3) To UBound(c3)
For d = LBound(c4) To UBound(c4)
For e = LBound(c5) To UBound(c5)
For f = LBound(c6) To UBound(c6)
For g = LBound(c7) To UBound(c7)
For h = LBound(c8) To UBound(c8)
Cells(n, 1).Value = c1(a)
Cells(n, 2).Value = c2(b)
Cells(n, 3).Value = c3(c)
Cells(n, 4).Value = c4(d)
Cells(n, 5).Value = c5(e)
Cells(n, 6).Value = c6(f)
Cells(n, 7).Value = c7(g)
Cells(n, 8).Value = c8(h)
n = n + 1
Next h
Next g
Next f
Next e
Next d
Next c
Next b
Next a
End With
End Sub
I have also found a recursive code sample on the internet but i really don't know how to modify according to my need. Any help would be really great.
Recursive code sample
Sub RecurseMe(a, v, depth)
If a > depth Then
PrintV v
Exit Sub
End If
For x = 1 To 4
v(a) = x
a = a + 1
RecurseMe a, v, depth
a = a - 1
Next x
End Sub
Sub PrintV(v)
For J = 1 To UBound(v): Debug.Print v(J) & " ";: Next J
Debug.Print
End Sub
Sub test()
Dim v()
depth = 4 'adjust
a = 1
ReDim v(1 To depth)
RecurseMe a, v, depth
End Sub
Regards