下棋法之数据透视表式汇总,列数计算方法示例2与13楼第四个例子相对照,比较列数如何确定
- Sub 下棋法之数据透视表式汇总()
- Set d = CreateObject("scripting.dictionary")
- Dim 棋盘(1 To 10000, 1 To 7)
- Dim 行数, 列数
- Dim arr, x, k
- arr = Range("a2:f" & Range("a65536").End(xlUp).Row)
- For x = 1 To UBound(arr)
- sr = arr(x, 1) & arr(x, 2) & arr(x, 3) & arr(x, 4)
- 列数 = (InStr("二库三库四库", arr(x, 6)) + 1) / 2 + 4
- If d.Exists(sr) Then
- 行数 = d(sr)
- 棋盘(行数, 列数) = 棋盘(行数, 列数) + arr(x, 5)
- Else
- k = k + 1
- d(sr) = k
- 棋盘(k, 1) = arr(x, 1)
- 棋盘(k, 2) = arr(x, 2)
- 棋盘(k, 3) = arr(x, 3)
- 棋盘(k, 4) = arr(x, 4)
- 棋盘(k, 列数) = arr(x, 5)
- End If
- Next x
- With Sheet2
- .Activate
- .Columns(1).NumberFormatLocal = "@"
- .Range("a1").Resize(1, 8) = Array("物品代码", "物品名称", "规格", "单位", "二库", "三库", "四库", "汇总")
- .Range("a2").Resize(k, 7) = 棋盘
- .Range("H2").FormulaR1C1 = "=SUM(RC[-3]:RC[-1])"
- .Range("H2").AutoFill Destination:=Range("H2:H" & k + 1), Type:=xlFillDefault
- End With
- End Sub
复制代码 |