|
- Sub qs()
- Dim arr, i, dic
- Set dic = CreateObject("scripting.dictionary")
- With Sheet1
- arr = .Range("a2").CurrentRegion.Value
- ReDim brr(1 To UBound(arr), 1 To UBound(arr, 2))
- For i = 1 To UBound(arr)
- s = arr(i, 1): s2 = arr(i, 2): s3 = arr(i, 3)
- If Not dic.exists(s) Then Set dic(s) = CreateObject("scripting.dictionary")
- If Not dic(s).exists(s2) Then Set dic(s)(s2) = CreateObject("scripting.dictionary")
- If Not dic(s)(s2).exists(s3) Then
- dic(s)(s2)(s3) = arr(i, 4)
- Else
- dic(s)(s2)(s3) = dic(s)(s2)(s3) + arr(i, 4)
- End If
- Next
- For Each k In dic.keys
- For Each k2 In dic(k).keys
- For Each k3 In dic(k)(k2).keys
- m = m + 1
- brr(m, 1) = k: brr(m, 2) = k2: brr(m, 3) = k3
- brr(m, 4) = dic(k)(k2)(k3)
- Next k3
- Next k2
- Next k
- .[n2:z1000].Clear
- .[n2].Resize(m, 4) = brr
- Application.DisplayAlerts = False
- rw = .Cells(Rows.Count, "n").End(3).Row
- For i = rw To 3 Step -1
- If .Range("n" & i) = .Range("n" & i - 1) Then
- Range("n" & i, .Range("n" & i - 1)).Merge
- Range("r" & i, .Range("r" & i - 1)).Merge
- End If
-
- If .Range("o" & i) = .Range("o" & i - 1) Then
- Range("o" & i, .Range("o" & i - 1)).Merge
- End If
- Next
- For i = 2 To rw
- If .Range("r" & i).MergeCells Then
- .Range("r" & i).Value = Application.WorksheetFunction.Sum(.Range("Q" & i).Resize(.Range("r" & i).MergeArea.Count))
- Else
- .Range("r" & i).Value = .Range("q" & i).Value
- End If
- Next
- ReDim crr(1 To 1, 1 To 5)
- crr(1, 1) = "合计": crr(1, 4) = Application.Sum(Application.Index(brr, 0, 4))
- .Range("n" & 2 + m).Resize(1, 5) = crr
- .Range("n" & 2 + m).Resize(1, 3).Merge
- With .Range("n2").CurrentRegion
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- Application.DisplayAlerts = True
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|