|
2级字典,表格转换- Sub ykcbf() '//2024.7.7 表格转换,2级字典
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("Scripting.Dictionary")
- bt = 2: col = 5
- With Sheets("Sheet1")
- r = .Cells(Rows.Count, col).End(3).Row
- arr = .[a1].Resize(r, 5)
- End With
- For i = bt + 1 To UBound(arr)
- s = arr(i, col)
- If Not d.exists(s) Then Set d(s) = CreateObject("Scripting.Dictionary")
- d(s)(i) = i
- Next
- ReDim brr(1 To 10000, 1 To 5)
- For Each k In d.keys
- n = 0: sum1 = 0: sum2 = 0
- For Each kk In d(k).keys
- n = n + 1
- m = m + 1
- brr(m, 1) = n
- brr(m, 2) = k
- brr(m, 3) = arr(kk, 2)
- brr(m, 4) = arr(kk, 3)
- brr(m, 5) = arr(kk, 4)
- sum1 = sum1 + Val(arr(kk, 3))
- sum2 = sum2 + Val(arr(kk, 4))
- Next
- s1 = s1 + sum1
- s2 = s2 + sum2
- m = m + 1
- brr(m, 1) = k & " " & "汇总"
- brr(m, 4) = sum1
- brr(m, 5) = sum2
- Next
- m = m + 1
- brr(m, 1) = "总计"
- brr(m, 4) = s1
- brr(m, 5) = s2
- With Sheets("Sheet2")
- .UsedRange.Offset(2).Clear
- With .[a3].Resize(m, 5)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- For i = 3 To m + 2
- If InStr(.Cells(i, 1), "汇总") Then
- .Cells(i, 1).Resize(, 5).Interior.ColorIndex = 8
- End If
- If InStr(.Cells(i, 1), "总计") Then
- .Cells(i, 1).Resize(, 5).Interior.ColorIndex = 6
- End If
- Next
- End With
- Set d = Nothing
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- MsgBox "OK!"
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|