|
- Sub 下棋法之数据透视表式汇总()
- Dim d As New Dictionary
- Dim 棋盘(1 To 10000, 1 To 8) '加个合计列
- Dim 行数, 列数
- Dim arr, x, k
- arr = Range("A2:C" & Range("C65536").End(xlUp).Row)
- For x = 1 To UBound(arr)
- 列数 = Left(arr(x, 2), 1) + 1 '获取月份转化为列数
- If d.Exists(arr(x, 1)) Then
- 行数 = d(arr(x, 1)) '从字典里取出产品类别对应的行数
- 棋盘(行数, 列数) = 棋盘(行数, 列数) + arr(x, 3) '同时汇总此月份费用
- Else
- k = k + 1 '依次向字典里存储产品类别对应的行数,A-1,B-2,C-3,...
- d(arr(x, 1)) = k
- 棋盘(k, 1) = arr(x, 1) '填入产品类别
- 棋盘(k, 列数) = arr(x, 3) '填入第一个费用(后续在此基础上累加)
- End If
- Next x
- For i = 1 To d.Count + 1
- For j = 2 To UBound(棋盘, 2) - 1
- 棋盘(i, 8) = 棋盘(i, 8) + 棋盘(i, j)
- If i <> d.Count + 1 Then 棋盘(d.Count + 1, j) = 棋盘(d.Count + 1, j) + 棋盘(i, j)
- Next
- Next
-
- 棋盘(d.Count + 1, 1) = "总计"
- Range("F2").Resize(k + 1, 8) = 棋盘
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|