|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Option Explicit
Function 分类汇总(arr, brr, rng As Range) ''brr选择任意列 [{2,3,4,5}]
Dim i, j, k, r As Range, dic选择列, key, dic单位, m, n, 行, 列, crr, dic, dic1, drr
Application.ScreenUpdating = False
Set dic = CreateObject("scripting.dictionary")
Set dic1 = CreateObject("scripting.dictionary")
Set dic单位 = CreateObject("scripting.dictionary")
Set dic选择列 = CreateObject("scripting.dictionary")
For i = LBound(arr) To UBound(arr)
If Not dic.Exists(key) Then
dic(arr(i, 1)) = ""
End If
For j = 2 To UBound(arr, 2)
If Not dic1.Exists(key) Then
dic1(arr(i, j)) = ""
End If
Next
Next
ReDim crr(1 To dic.Count + 2, 1 To dic1.Count + 1)
crr(1, 1) = arr(1, 1)
ReDim drr(1 To UBound(brr) + 1)
drr(1) = 1
For j = 1 To UBound(brr)
For i = 2 To UBound(arr)
key = arr(i, 1)
If j = 1 Then
If Not dic单位.Exists(key) Then
m = m + 1
dic单位(key) = m + 2
crr(m + 2, 1) = key
End If
End If
行 = dic单位(key)
key = arr(i, brr(j))
If Not dic选择列.Exists(key) Then
n = n + 1
dic选择列(key) = n + 1
crr(1, n + 1) = arr(1, brr(j))
crr(2, n + 1) = key
End If
列 = dic选择列(key)
crr(行, 列) = Val(crr(行, 列)) + 1
Next
drr(j + 1) = n + 1
Next
crr(m + 3, 1) = "合计"
For i = 3 To m + 2
For j = 2 To n + 1
crr(m + 3, j) = crr(m + 3, j) + crr(i, j)
Next
Next
With rng.Resize(m + 3, n + 1)
.Clear
.Value = crr
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
End With
rng.Resize(2, 1).Merge
With rng.Parent
For j = 2 To UBound(drr)
.Range(Cells(1, drr(j - 1) + 1), Cells(1, drr(j))).Merge
Next
End With
Application.ScreenUpdating = True
End Function
Sub text()
Dim arr, t
t = Timer
arr = Sheet1.[a1].CurrentRegion
分类汇总 arr, [{2,3,4,5}], Sheet2.[a1]
MsgBox "完成 ! 耗时 : " & Format(Timer - t, "0.00" & "秒"), , "提示"
End Sub
|
|