|
Sub 分级汇总()
ReDim Str(1 To 3), s(1 To 3)
s(1) = "选中“级数”起始单元格"
s(2) = "选中“值”所在列单元格"
s(3) = "选中“结果”所在列单元格"
For i = 1 To 3
Str(i) = Application.InputBox(s(i), Type:=8).Address
With CreateObject("vbscript.regexp")
If i = 1 Then
.Pattern = ".+(\d+)"
qd = Val(.Replace(Str(1), "$1"))
End If
.Pattern = "^\$([A-Z]+).*"
Str(i) = .Replace(Str(i), "$1")
End With
Next
c_ji = Str(1)
c_zhi = Str(2)
c_xie = Str(3)
zd = Range(c_ji & 65536).End(xlUp).Row
Set dic = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
Cells.ClearOutline
For i = qd To zd
j_now = Range(c_ji & i)
j_next = Range(c_ji & i + 1)
ActiveSheet.Rows(i).OutlineLevel = j_now
If j_now < j_next Then
dic(i) = j_now
ElseIf j_now > j_next Then
arr = dic.keys
For j = UBound(arr) To LBound(arr) Step -1
If dic(arr(j)) < j_next Then Exit For
With Range(c_xie & arr(j))
.Formula = "=subtotal(9," & c_xie & arr(j) + 1 & ":" & c_xie & i & ")"
End With
dic.Remove arr(j)
Next
End If
Next
ActiveSheet.Outline.SummaryRow = xlAbove
Columns(c_xie).Clear
Set dic = Nothing
Application.ScreenUpdating = True
End Sub |
评分
-
1
查看全部评分
-
|