|
- Sub 分类汇总()
- Dim d As Object, arA, arB, arC, k()
- Dim i&, x&, y&, j%, c%, p%, x1&, x2&, n As Double, n1 As Double, n2 As Double
- Set d = CreateObject("Scripting.Dictionary")
- arC = Sheet2.Range("n1").CurrentRegion
- Range("a1").CurrentRegion.Offset(2).ClearContents
- For i = 3 To UBound(arC)
- ReDim k(2)
- For x = 2 To 4
- k(x - 2) = arC(i, x)
- Next
- d(arC(i, 1)) = k
- Next
- arA = Sheet2.Range("a3:i" & Sheet2.Cells(Rows.Count, 1).End(3).Row + 1)
- ReDim arB(1 To UBound(arA) + d.Count * 5, 1 To UBound(arA, 2))
- For i = 1 To UBound(arA) - 1
- If i = 1 Then x1 = arA(i, 1)
- n = n + arA(i, 8)
- y = y + 1
- For x = 1 To UBound(arA, 2)
- arB(y, x) = arA(i, x)
- Next
- If arA(i, 2) <> arA(i + 1, 2) Then
- j = Application.CountA(d(arA(i, 2)))
- c = c + 1
- x2 = arA(i, 1)
- If j > 0 Then
- y = y + 1
- p = p + 1
- arB(y, 3) = "(" & p & ")" & arA(i, 2) & "小计(" & c & "):" & x1 & "-" & x2 & "项之和"
- arB(y, 8) = n
- k = d(arA(i, 2))
- For x = 0 To UBound(k)
- If Trim(k(x)) <> "" Then
- y = y + 1
- p = p + 1
- arB(y, 3) = "(" & p & ")" & Replace(arC(2, x + 2), "率", "") & "小计×" & k(x) * 100 & "%"
- arB(y, 8) = Round(n * k(x), 2)
- n1 = n1 + arB(y, 8)
- End If
- Next
- End If
- y = y + 1
- arB(y, 3) = IIf(p = 0, arA(i, 2), "") & "合计(" & c & "):" & IIf(p > 0, "(1)-(" & p & ")之和", x1 & "-" & x2 & "项之和")
- arB(y, 8) = n + n1
- n2 = n2 + arB(y, 8)
- n = 0
- n1 = 0
- p = 0
- x1 = arA(i + 1, 1)
- End If
- Next
- y = y + 2
- arB(y, 3) = "总计:合计(1)-(" & c & ")之和"
- arB(y, 8) = n2
- Range("a3").Resize(y, UBound(arB, 2)) = arB
- End Sub
复制代码 |
|