|
代码如下。。。
Sub test1()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim d
Dim dic
Dim brr(1 To 10000, 1 To 100)
Dim Brow, Bcol
Dim arr, x, k, kk, kkk
Set d = CreateObject("scripting.dictionary")
Set dic = CreateObject("scripting.dictionary")
With Sheet4
arr = .Range("a1:e" & .Range("a65536").End(xlUp).Row)
For i = 2 To UBound(arr)
s = arr(i, 2)
If Not d.Exists(s) Then Set d(s) = CreateObject("scripting.dictionary")
For j = 4 To 5
ss = arr(i, 3) & "|" & arr(1, j)
sss = arr(i, j)
If sss <> Empty Then
d(s)(ss) = sss
If Not dic.Exists(arr(1, j)) Then Set dic(arr(1, j)) = CreateObject("scripting.dictionary")
dic(arr(1, j))(arr(i, 3)) = ""
End If
Next
Next
brr(1, 1) = "序号": brr(1, 2) = "姓名"
x = 2
For Each k In d.Keys
x = x + 1
brr(x, 1) = x - 2
brr(x, 2) = k
j = 2: Sum = 0
For Each k1 In dic.Keys
For Each k2 In dic(k1).Keys
If d(k).Exists(k2 & "|" & k1) Then
j = j + 1
brr(1, j) = k1
brr(2, j) = k2
brr(x, j) = d(k)(k2 & "|" & k1)
Sum = Sum + brr(x, j)
End If
Next
j = j + 1
brr(1, j) = k1
brr(2, j) = "小计"
brr(x, j) = "=sum(rc[-1]:rc[" & -dic(k1).Count & "])"
Next
brr(x, j + 1) = Sum
brr(1, j + 1) = "合计"
y = Application.Max(y, j + 1)
Next
End With
With ThisWorkbook.Sheets("案例")
.[a1].CurrentRegion.Clear
.[a1].Resize(x, y) = brr
.[a1].Resize(2, 1).Merge
.[b1].Resize(2, 1).Merge
.[a1].Offset(, y - 1).Resize(2, 1).Merge
.Cells(x + 1, "a").Resize(, 2).Merge
.Cells(x + 1, "a") = "合计"
For i = y - 1 To 4 Step -1
If .Cells(1, i) = .Cells(1, i - 1) Then .Range(.Cells(1, i), .Cells(1, i - 1)).Merge
Next
.[a1].CurrentRegion.HorizontalAlignment = xlCenter
.[a1].CurrentRegion.Borders.LineStyle = 1
.[a1].CurrentRegion.Font.Size = 10
.Cells(x + 1, "c").Resize(, y - 2) = "=sum(r[-1]c:r[" & -x + 2 & "]c)"
End With
Set d = Nothing
Set dic = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Beep
End Sub
|
|