bonjour,
une solution possible (sans contrainte)
Sub aargh()
With ActiveSheet
For i = 5 To 11 'lignes commandes
refc = .Cells(i, 3) 'reference commande
qc = .Cells(i, 4) 'quantité commandée
col = 3 'pointeur de colonne pour solution
For j = 17 To 25 'lignes stock
qs = .Cells(j, 4) 'quantité en stock dans cet entrepot
If .Cells(j, 2) = refc And qs > 0 Then
If qs > qc Then
qe = qc 'quantité à sortir de l'entrepot
.Cells(j, 4) = qs - qc 'calcul nouveau stock
Else
qe = qs ' 'quantité à sortir de l'entrepot
.Cells(j, 4) = 0 'calcul nouveau stock
End If
qc = qc - qe 'quantité restant à commander
col = col + 2
.Cells(i, col) = .Cells(j, 3) 'entrepot
.Cells(i, col + 1) = qe 'quantité à sortir
If qc = 0 Then Exit For 'commande satisfaite ?
End If
Next j
If qc <> 0 Then
col = col + 1
.Cells(i, col) = "pas assez de stock"
End If
Next i
End With
End Sub