Option Explicit
Sub PartitionnerListeCourt()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Feuil1")
Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim elems() As Variant, poids() As Variant
elems = ws.Range("A1:A" & lastRow).Value
poids = ws.Range("B1:B" & lastRow).Value
' Collection de paquets
Dim paquets As Collection: Set paquets = New Collection
Dim i As Long, j As Long, trouve As Boolean
For i = 1 To UBound(poids, 1)
trouve = False
For j = 1 To paquets.Count
Dim p As Collection: Set p = paquets(j)
Dim s As Double: s = 0
Dim e As Variant
For Each e In p: s = s + e(1): Next
If s + poids(i, 1) <= 39 Then
Dim arr(1 To 2): arr(1) = poids(i, 1): arr(2) = elems(i, 1)
p.Add arr
trouve = True
Exit For
End If
Next j
If Not trouve Then
Dim np As Collection: Set np = New Collection
Dim arr2(1 To 2): arr2(1) = poids(i, 1): arr2(2) = elems(i, 1)
np.Add arr2
paquets.Add np
End If
Next i
' Affichage
Dim ligne As Long: ligne = 1
For i = 1 To paquets.Count
Dim sumP As Double: sumP = 0
Dim LigCol As String: LigCol = ""
For Each e In paquets(i)
LigCol = LigCol & e(2) & " "
sumP = sumP + e(1)
Next
If sumP > 38 And sumP < 39 Then
ws.Cells(ligne, 4).Value = "Paquet " & i
ws.Cells(ligne, 5).Value = LigCol
ws.Cells(ligne, 6).Value = sumP
ligne = ligne + 1
End If
Next i
End Sub