|
如果汇总表格的横竖表头固定的话,也可以使用数组的办法进行汇总。- Sub test4()
- Dim d As Object
- Dim d1 As Object
- Dim r%, i%
- Dim arr, brr
- tt = Timer
- Set d = CreateObject("scripting.dictionary")
- Set d1 = CreateObject("scripting.dictionary")
- Set d2 = CreateObject("scripting.dictionary")
- With Worksheets("求助这种格式的代码")
- c = .Cells(4, .Columns.Count).End(xlToLeft).Column
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- .Range("b5").Resize(r - 4, c - 1).ClearContents
- arr = .Range("a3").Resize(r - 3, c)
- End With
- For j = 4 To UBound(arr, 2) Step 2
- d1(arr(1, j)) = j
- Next
- For i = 3 To UBound(arr)
- d2(arr(i, 1)) = i
- Next
- With Worksheets("台帐")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- brr = .Range("a2:e" & r)
- End With
-
- For i = 1 To UBound(brr)
- If d1.Exists(brr(i, 2)) And d2.Exists(Month(brr(i, 1))) Then
- r = d2(Month(brr(i, 1)))
- c = d1(brr(i, 2))
- arr(r, c) = arr(r, c) + brr(i, 3)
- arr(r, c + 1) = arr(r, c + 1) + brr(i, 5)
- arr(r, 2) = arr(r, 2) + brr(i, 3)
- arr(r, 3) = arr(r, 3) + brr(i, 5)
- End If
- Next
-
- With Worksheets("求助这种格式的代码")
- .UsedRange.Offset(4, 0).ClearContents
- .Range("a3").Resize(UBound(arr), UBound(arr, 2)) = arr
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- .Cells(r + 1, 1) = "合计"
- .Cells(r + 1, 2).Resize(1, 12).FormulaR1C1 = "=SUM(R[" & 4 - r & "]C:R[-1]C)"
- End With
- MsgBox Timer - tt
- End Sub
复制代码 |
|