|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 分类汇总()
Application.ScreenUpdating = False
Dim d As Object
Dim ar As Variant
Dim br()
tt = Timer
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
With Sheets("Sheet1")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 4 Then MsgBox "数据源为空!": End
ar = .Range("A1:ab" & r)
End With
For i = 4 To UBound(ar)
If ar(i, 1) <> "" Then
If IsDate(ar(i, 1)) Then
s = Month(ar(i, 1))
If Not d.Exists(s) Then Set d(s) = CreateObject("scripting.dictionary")
d(s)(i) = i
End If
End If
Next i
With Sheets("实现效果")
.UsedRange.Offset(3).Borders.LineStyle = 0
.UsedRange.Offset(3).Interior.ColorIndex = 0
.UsedRange.Offset(3) = Empty
For Each k In d.keys
m = 0
dc.RemoveAll
ReDim br(1 To UBound(ar), 1 To UBound(ar, 2))
For Each kk In d(k).keys
zd = ar(kk, 1) & "|" & ar(kk, 11)
t = dc(zd)
If t = "" Then
m = m + 1
dc(zd) = m
t = m
For j = 1 To 10
br(m, j) = ar(kk, j)
Next j
For j = 11 To 21 Step 2
br(m, j) = ar(kk, j)
Next j
br(m, 23) = ar(kk, 23)
End If
For j = 12 To 22 Step 2
br(t, j) = br(t, j) + ar(kk, j)
Next j
For j = 24 To 28 Step 1
br(t, j) = br(t, j) + ar(kk, j)
Next j
Next kk
rs = .Cells(Rows.Count, 1).End(xlUp).Row + 1
If rs < 4 Then
rs = 4
Else
rs = rs
End If
.Cells(rs, 1).Resize(m, UBound(br, 2)) = br
.Cells(rs, 1).Resize(m + 1, UBound(br, 2)).Borders.LineStyle = 1
.Cells(rs + m, 1) = k & " 汇总"
.Cells(rs + m, 1).Resize(1, UBound(br, 2)).Interior.ColorIndex = 23
For j = 12 To 22 Step 2
.Cells(rs + m, j) = Application.Sum(Application.Index(br, 0, j))
Next j
For j = 24 To 28 Step 1
.Cells(rs + m, j) = Application.Sum(Application.Index(br, 0, j))
Next j
Next k
End With
Application.ScreenUpdating = True
MsgBox "用时" & Format(Timer - tt, "0.00") & "秒"
End Sub
|
|