什么叫做『棋盘法』汇总?
'功能:VBA字典棋盘法-多列汇总
'公众号:Excel办公实战
'作者:E精精
'日期:20210609
'----------------------------------------------------
Sub dataTotal()
'------------数据源装入数组-------------
Dim lRow As Long, arr, brr(1 To 1000, 1 To 3)
With Sheet1
lRow = .Cells(Rows.Count, 1).End(3).Row
arr = .Range("A2:D" & lRow).Value
End With
'---------结果字典、循环处理-----------
Dim d As Object '申明字典变量
'后期绑定
Set d = CreateObject("scripting.dictionary")
Dim i As Long, skey As String, n As Long
Dim totalRow As Long
For i = 1 To UBound(arr)
skey = arr(i, 2) '销售名称
If Not d.exists(skey) Then
n = n + 1
totalRow = n
d(skey) = n
'首次写入销售名称
brr(totalRow, 1) = skey
Else
totalRow = d(skey)
End If
'数量汇总
brr(totalRow, 2) = brr(totalRow, 2) + arr(i, 3)
'金额汇总
brr(totalRow, 3) = brr(totalRow, 3) + arr(i, 4)
Next
'写入结果
With Sheet1
.Range("F21:Z1000").Clear
.Range("F21").Resize(n, 3) = brr
End With
MsgBox "处理完成"
End Sub
赞 (0)