这东西得用代码,因要插入行.
第2点和这差不多,自己改一下.
Sub req1()
Dim tmpRr As Range, res As Variant, tar As Variant, outArr() As Variant, tmpRt As Range, i%, j%
Set tmpRr = [B65536].End(xlUp)
Set tmpRr = Range([B4], tmpRr)
Set tmpRt = [E65536].End(xlUp)
Set tmpRt = Range([E4], tmpRt)
i = tmpRr.Rows.Count
res = tmpRr.Resize(i, 3)
ReDim outArr(1 To i + tmpRt.Rows.Count, 1 To 6)
i = tmpRt.Rows.Count
tar = tmpRt.Resize(i, 3)
k = 1
For i = 1 To i
j = j + 1
outArr(j, 1) = res(i, 1)
outArr(j, 2) = res(i, 2)
outArr(j, 3) = res(i, 3)
outArr(j, 4) = tar(k, 1)
outArr(j, 6) = tar(k, 3)
If res(i, 2) >= tar(k, 2) Then
outArr(j, 5) = tar(k, 2)
res(i, 2) = res(i, 2) - tar(k, 2)
If res(i, 2) > 0 Then i = i - 1
If k >= tmpRt.Rows.Count Then
Exit For
Else
k = k + 1
End If
Else
outArr(j, 5) = res(i, 2)
tar(k, 2) = tar(k, 2) - res(i, 2)
End If
Next
[I21].Resize(j, 6) = outArr
End Sub
|