Dear madam/sir,
I'm trying to loop over all possible combinations when choosing c items out of a possible n. This is easily done with nested loops, as my example shown below. However, the number of for-loops needed depends on the input parameter of c (the number chosen).
I'm scratching my head but can't figure a way out anymore on how to set 'dynamically' (not sure if thats the proper term) the number of loops.
Here's my example code:
Any suggestions would be highly appreciated.
I'm trying to loop over all possible combinations when choosing c items out of a possible n. This is easily done with nested loops, as my example shown below. However, the number of for-loops needed depends on the input parameter of c (the number chosen).
I'm scratching my head but can't figure a way out anymore on how to set 'dynamically' (not sure if thats the proper term) the number of loops.
Here's my example code:
VBA Code:
Option Explicit
Sub example()
Dim c, n, r, i, j, k, l As Integer
n = 8
c = 4
r = 1
For i = 1 To n - c + 1
For j = i + 1 To n - c + 2
For k = j + 1 To n - c + 3
For l = k + 1 To n - c + 4
Cells(r, 1) = i
Cells(r, 2) = j
Cells(r, 3) = k
Cells(r, 4) = l
r = r + 1
Next l
Next k
Next j
Next i
End Sub
Any suggestions would be highly appreciated.