Bonjour,
une proposition de solution
Sub aargh()
Dim a
Dim s
dl = Cells(Rows.Count, 1).End(xlUp).Row
With Range("A2:B" & dl)
'.Sort key1:=Range("b1"), order1:=xlAscending, Header:=xlNo
a = .Value
m = Application.WorksheetFunction.Sum(Range("B2:B" & dl)) / 2
t = 0
ReDim s(LBound(a, 1) To UBound(a, 1))
totpoids sol, s, m, a
s = Split(sol, ",")
k1 = 1
k2 = k1
For i = LBound(s) To UBound(s)
If s(i) <> 0 Then
k1 = k1 + 1
Cells(k1, 3) = Cells(i + 2, 1)
Cells(k1, 4) = Cells(i + 2, 2)
Else
k2 = k2 + 1
Cells(k2, 5) = Cells(i + 2, 1)
Cells(k2, 6) = Cells(i + 2, 2)
End If
Next i
End With
End Sub
Sub totpoids(ByRef sol, ByRef s, m, a, Optional t = 0, Optional j = 1, Optional n = 1, Optional max = 1000000#)
For i = j To UBound(a, 1)
t = t + a(i, 2)
s(i) = 1
If n < UBound(a, 1) / 2 Then
totpoids sol, s, m, a, t, i + 1, n + 1, max
ElseIf Abs(t - m) < max Then
max = Abs(t - m)
sol = Join(s, ",")
End If
t = t - a(i, 2)
s(i) = 0
Next i
End Sub