我的问题是针对 VBA Excel。我有一个与此类似的数据集:(已编辑)
Order Number Description Item Code Value
AA000001 Mopping Service Payment 00001 100.00
AA000001 Mopping Service Discount 00001 -50.00
AA000001 Bucket Rental 00002 50.00
AA000001 Bucket Rental Discount 00002 -25.00
AA000001 Mopping Service Payment 00001 25.00
AA000001 Bucket Rental 00002 10.00
AA000002 Mopping Service Payment 00001 100.00
AA000002 Mopping Service Discount 00001 -50.00
AA000002 Bucket Rental 00002 50.00
AA000002 Bucket Rental Discount 00002 -25.00
我想要的输出:
Order Number Description Item Code Value
AA000001 Mopping Service Payment 00001 75.00
AA000001 Bucket Rental 00002 35.00
AA000002 Mopping Service Payment 00001 50.00
AA000002 Bucket Rental 00002 25.00
我在互联网上找到了以下代码,并对其进行了一些修改,但我的问题是它没有逻辑仅用于基于订单号组合重复项(相反,它用相同的值替换所有项目代码,而不管订单编号。)有没有办法添加代码以获取与给定订单号相似的所有项目代码并将它们相加?
我需要添加什么?我错过了什么?提前致谢!
Dim Sh As Worksheet
Dim LastRow As Long
Dim Rng As Range
Set Sh = Worksheets(1)
Sh.Columns(5).Insert
LastRow = Sh.Range("A65536").End(xlUp).Row
With Sh.Range("A1:A" & LastRow).Offset(0, 4)
.FormulaR1C1 = "=IF(COUNTIF(R1C[-2]:RC[-2],RC[-2])>1,"""",SUMIF(R1C[-2]:R[" & LastRow & "]C[-2],RC[-2],R1C[-1]:R[" & LastRow & "]C[-1]))"
.Value = .Value
End With
Sh.Columns(4).Delete
Sh.Rows(1).Insert
Set Rng = Sh.Range("D1:D" & LastRow + 1)
With Rng
.AutoFilter Field:=1, Criteria1:="="
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With