|
本帖最后由 ykcbf1100 于 2024-5-19 10:13 编辑
增加小计和合计- Sub ykcbf2() '//2024.5.19 增加小计、增加总计
- Dim arr, d
- Set d = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim tm: tm = Timer
- Set ws = ThisWorkbook
- Set sh = ws.Sheets("资金支付查询20240517124124763")
- bt = 3: col = 25
- For Each sht In Sheets
- If sht.Name <> sh.Name Then sht.Delete
- Next
- arr = sh.UsedRange
- For i = bt + 1 To UBound(arr)
- s = arr(i, col): ss = arr(i, 32)
- If Not d.Exists(s) Then Set d(s) = CreateObject("scripting.dictionary")
- If Not d(s).Exists(ss) Then Set d(s)(ss) = CreateObject("scripting.dictionary")
- d(s)(ss)(i) = i
- Next i
- On Error Resume Next
- For Each k In d.keys
- ReDim hj(1 To 2)
- ReDim brr(1 To UBound(arr), 1 To UBound(arr, 2))
- sh.Copy after:=Sheets(Sheets.Count)
- Set sht = Sheets(Sheets.Count)
- m = 0
- With sht
- .Name = k
- .UsedRange.Offset(bt).ClearContents
- .DrawingObjects.Delete
- .Range("d:e,v:v").NumberFormatLocal = "@"
- For Each kk In d(k).keys
- ReDim Sum(1 To 2)
- For Each kkk In d(k)(kk).keys
- m = m + 1
- For j = 1 To UBound(arr, 2)
- brr(m, j) = arr(kkk, j)
- Next
- Sum(1) = Sum(1) + brr(m, 8)
- Sum(2) = Sum(2) + brr(m, 9)
- Next
- m = m + 1
- brr(m, 8) = Sum(1): brr(m, 32) = Split(brr(m - 1, 32), "-")(1) & " 小计"
- brr(m, 9) = Sum(2)
- hj(1) = hj(1) + Sum(1)
- hj(2) = hj(2) + Sum(2)
- Next
- m = m + 1
- brr(m, 8) = hj(1): brr(m, 32) = "总计"
- brr(m, 9) = hj(2)
- .[a4].Resize(m, UBound(arr, 2)) = brr
- r = .Cells(Rows.Count, col).End(3).Row
- .UsedRange.Offset(r + 2).Clear
- .Rows(3).Delete
- End With
- Next k
- sh.Activate
- Set d = Nothing
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "共用时:" & Format(Timer - tm) & "秒!"
- End Sub
复制代码
|
|