|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim d As Object
- Set d = CreateObject("scripting.dictionary")
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:c" & r)
- For i = 1 To UBound(arr)
- xm = Split(arr(i, 1), " ")
- If Not d.exists(xm(0)) Then
- Set d(xm(0)) = CreateObject("scripting.dictionary")
- End If
- If Not d(xm(0)).exists(xm(1)) Then
- ReDim brr(1 To 4)
- brr(1) = xm(1)
- brr(4) = arr(i, 2)
- Else
- brr = d(xm(0))(xm(1))
- End If
- brr(3) = brr(3) + arr(i, 3)
- d(xm(0))(xm(1)) = brr
- Next
- .Range("n2:q" & .Rows.Count).Clear
- r = 2
- For Each aa In d.keys
- .Cells(r, 14) = aa & "小计"
- ReDim crr(1 To d(aa).Count, 1 To 4)
- m = 0
- xj = 0
- For Each bb In d(aa).keys
- brr = d(aa)(bb)
- m = m + 1
- For j = 1 To UBound(brr)
- crr(m, j) = brr(j)
- Next
- xj = xj + brr(3)
- Next
- .Cells(r, 15) = xj
- .Cells(r + 1, 14).Resize(UBound(crr), UBound(crr, 2)) = crr
- With .Cells(r, 14).Resize(1 + UBound(crr), UBound(crr, 2))
- .Borders.LineStyle = xlContinuous
- With .Font
- .Name = "微软雅黑"
- .Size = 10
- End With
- End With
- r = r + 1 + UBound(crr)
- Next
- End With
- End Sub
复制代码 |
|