Dim Cb()
Sub Main(): Call AllCombins(7, 2, 0, False): End Sub
Private Sub AllCombins(ByVal a%, Optional Debut% = 1, Optional Fin% = 0, Optional SortieNumeric As Boolean = False)
Dim i%, j&, r&, k%
Application.ScreenUpdating = False: Application.Calculation = xlCalculationManual
If Fin = 0 Then Fin = a
If Debut < 1 Or Fin < Debut Or Fin > a Then Exit Sub
For i = Debut To Fin
ReDim Cb(i)
For j = 1 To i - 1: Cb(j) = j: Next j
Cb(j) = Cb(j - 1)
For j = 1 To CombinNb(a, i)
Call CombinNext(a)
r = r + 1
If SortieNumeric Then
For k = 1 To i: Cells(r, k) = Cb(k): Next k
Else
Cells(r, 1) = Join(Cb)
End If
Next j
Next i
Application.Calculation = xlCalculationAutomatic
End Sub
Private Sub CombinNext(ByVal a%)
Dim b%, i%
b = UBound(Cb)
If b = a Then Cb(b) = Cb(b) + 1: Exit Sub
If b = 1 Then Cb(1) = Cb(1) + 1 Else Cb(1) = Cb(1) - (Cb(2) = a - b + 2)
For i = 2 To b - 1
If Cb(i + 1) = a - b + i + 1 Then Cb(i) = Cb(i + (Cb(i) = a - b + i)) + 1
Next i
If b > 1 Then
If Cb(b) = a Then Cb(b) = Cb(b - 1) + 1 Else Cb(b) = Cb(b) + 1
End If
End Sub
Private Function CombinNb(ByVal a%, ByVal b%) As Double
Dim c%
c = a - b
If b < c Then c = b
If c = 0 Then CombinNb = 1 Else CombinNb = MathFactoriel(a, c) / MathFactoriel(c)
End Function
Private Function MathFactoriel(ByVal Nb%, Optional Iter% = 0) As Double
Dim i&, n&
If Iter = 0 Then Iter = Nb
MathFactoriel = 1
For i = 0 To Iter - 1: MathFactoriel = MathFactoriel * (Nb - i): Next i
End Function