Option Explicit
Dim dicoexclu1 As Object, dicoexclu2 As Object, dicogarder As Object 'mémorise les variables
Function CoupleGarder(x%, a%, Optional b%, Optional c%, Optional d%) As Boolean
If dicogarder.exists(a & " " & x) Then CoupleGarder = True: Exit Function
If b = 0 Then Exit Function Else If dicogarder.exists(b & " " & x) Then CoupleGarder = True: Exit Function
If c = 0 Then Exit Function Else If dicogarder.exists(c & " " & x) Then CoupleGarder = True: Exit Function
If d Then If dicogarder.exists(d & " " & x) Then CoupleGarder = True
End Function
Function CoupleExclu(x%, a%, Optional b%, Optional c%, Optional d%) As Boolean
If dicoexclu1.exists(a & " " & x) Then CoupleExclu = True: Exit Function
If b = 0 Then Exit Function
If dicoexclu1.exists(b & " " & x) Then CoupleExclu = True: Exit Function
If c = 0 Then Exit Function
If dicoexclu1.exists(c & " " & x) Then CoupleExclu = True: Exit Function
If d Then If dicoexclu1.exists(d & " " & x) Then CoupleExclu = True
End Function
Function TripleExclu(x%, a%, b%, Optional c%, Optional d%) As Boolean
If dicoexclu2.exists(a & " " & b & " " & x) Then TripleExclu = True: Exit Function
If c = 0 Then Exit Function
If dicoexclu2.exists(a & " " & c & " " & x) Then TripleExclu = True: Exit Function
If dicoexclu2.exists(b & " " & c & " " & x) Then TripleExclu = True: Exit Function
If d = 0 Then Exit Function
If dicoexclu2.exists(a & " " & d & " " & x) Then TripleExclu = True: Exit Function
If dicoexclu2.exists(b & " " & d & " " & x) Then TripleExclu = True: Exit Function
If dicoexclu2.exists(c & " " & d & " " & x) Then TripleExclu = True
End Function
Sub Combinaisonselimination()
Dim nmax%, exclu1, exclu2, garder, Ncombi&, rc&, tablo$(), m%, n%, o%, p%, q%, g1, g2, g3, g4, lig&, col%
nmax = 25 'modifiable
lig = 1
exclu1 = [I1].CurrentRegion.Resize(, 2) 'couples
exclu2 = [L1].CurrentRegion.Resize(, 3) 'triples
garder = Array("1 13")
Ncombi = Application.Combin(nmax, 5) 'nombre de combinaisons
rc = Rows.Count
ReDim tablo(1 To rc, 0 To Int(Ncombi / rc))
Set dicoexclu1 = CreateObject("Scripting.Dictionary")
Set dicoexclu2 = CreateObject("Scripting.Dictionary")
Set dicogarder = CreateObject("Scripting.Dictionary")
For m = 1 To UBound(exclu1): dicoexclu1(exclu1(m, 1) & " " & exclu1(m, 2)) = "": Next
For m = 1 To UBound(exclu2): dicoexclu2(exclu2(m, 1) & " " & exclu2(m, 2) & " " & exclu2(m, 3)) = "": Next
For m = 0 To UBound(garder): dicogarder(garder(m)) = "": Next
For m = 1 To nmax - 4
For n = m + 1 To nmax - 3
If CoupleExclu(n, m) Then GoTo 1
g1 = CoupleGarder(n, m)
For o = n + 1 To nmax - 2
If g1 Then g2 = True Else g2 = CoupleGarder(o, m, n)
If CoupleExclu(o, m, n) Or TripleExclu(o, m, n) Then GoTo 2
For p = o + 1 To nmax - 1
If g2 Then g3 = True Else g3 = CoupleGarder(p, m, n, o)
If CoupleExclu(p, m, n, o) Or TripleExclu(p, m, n, o) Then GoTo 3
For q = p + 1 To nmax
If g3 Then g4 = True Else g4 = CoupleGarder(q, m, n, o, p)
If CoupleExclu(q, m, n, o, p) Or TripleExclu(q, m, n, o, p) Then GoTo 4
If g4 Then
tablo(lig, col) = m & " " & n & " " & o & " " & p & " " & q
lig = lig + 1
If lig = rc Then lig = 0: col = col + 1
End If
End If
4 Next q
3 Next p
2 Next o
1 Next n
Next m
[P1].CurrentRegion.ClearContents 'RAZ
If lig Or col Then [P1].Resize(IIf(col, rc, lig), col + 1) = tablo
Columns("P").Resize(, col + 1).AutoFit 'ajustement largeur
End Sub