أخي العزيز
ممكن تستبدل بهذا الكود عسى ولعل
Sub test()
Dim ws As Worksheet: Set ws = Sheets("توزيع المبالغ")
Dim sh As Worksheet: Set sh = Sheets("تجميع المبالغ")
Dim a, b, c, m, d
Dim x#
Dim i&, ii&
a = ws.Cells(6, 7).CurrentRegion
b = sh.Cells(6, 1).CurrentRegion.Offset(2).Columns(1)
ReDim c(1 To UBound(b) - 2, 1 To 2)
m = ws.Range("E1")
For i = 2 To UBound(a)
For ii = 6 To UBound(a, 2)
If (a(i, ii)) = "" Then Exit For
x = Application.Match(a(i, ii), b, 0)
c(x, 1) = IIf(c(x, 1) = "", a(i, 2), c(x, 1) & " + " & a(i, 2))
If a(i, 4) <= m Then
c(x, 2) = c(x, 2) + a(i, 4)
Else
c(x, 2) = c(x, 2) + m: a(i, 4) = a(i, 4) - m
End If
Next
Next
d = sh.Range(sh.Cells(5, 4), sh.Cells(5, 4).End(xlToRight)).Value
On Error Resume Next
For i = 1 To UBound(d, 2): d(1, i) = 1 * Split(d(1, i), "/")(0): Next
d = Application.Transpose(d)
x = Application.Match(1 * Split(ws.Range("E7"), "/")(0), d, 0)
With sh
.Cells(6, 2).Resize(UBound(c)) = c
.Cells(6, x + 3).Resize(UBound(c)) = Application.Index(c, 0, 2)
End With
End Sub