
Option Explicit
Sub abc()
Dim a(1), i, j, p, n, d
a(0) = Range("k3:n" & [k2].End(xlDown).Row).Value
Set d = CreateObject("scripting.dictionary")
For i = 1 To UBound(a(0))
If Not d.exists(a(0)(i, 1)) Then d(a(0)(i, 1)) = i Else MsgBox "!": Exit Sub
Next
a(1) = [a1].CurrentRegion.Offset(1).Resize(, 6).Value
ReDim b(1 To UBound(a(1)) - 1, 1 To 2)
For i = 1 To UBound(a(1)) - 1
If a(1)(i, 1) <> a(1)(i + 1, 1) Then
If d.exists(a(1)(i, 1)) Then
n = a(0)(d(a(1)(i, 1)), 4)
For j = p + 1 To i
b(j, 2) = a(0)(d(a(1)(i, 1)), 3)
If n > a(1)(j, 6) Then
b(j, 1) = a(1)(j, 6): n = n - b(j, 1)
Else
b(j, 1) = n: Exit For
End If
Next
End If
p = i
End If
Next
[g2].Resize(UBound(b), 2) = b
End Sub