|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 ykcbf1100 于 2024-6-21 05:06 编辑
汇总表标题行自动生成、纵向合计和横向合计自动生成。- Sub ykcbf() '//2024.6.21
- Application.ScreenUpdating = False
- Dim arr, brr, d
- Set d = CreateObject("Scripting.Dictionary")
- Set ws = ThisWorkbook
- Set sh = ws.Sheets("汇总")
- ReDim brr(1 To 1000, 1 To 16)
- m = 1: n = 2
- brr(1, 1) = "序号": brr(1, 2) = "姓名": brr(1, 16) = "年度合计"
- On Error Resume Next
- For x = 2 To ws.Sheets.Count
- With ws.Sheets(x)
- r = .UsedRange.Rows.Count
- Set Rng = .UsedRange.Find("打卡金额")
- r1 = Rng.Row: c1 = Rng.Column
- arr = .Range("a1:l" & r)
- fn = .Name
- End With
- For i = r1 + 1 To UBound(arr)
- s = Replace(arr(i, 3), " ", "")
- If Val(arr(i, 1)) Then
- If Not d.exists(s) Then
- m = m + 1
- d(s) = m
- brr(m, 1) = m - 1
- brr(m, 2) = s
- End If
- r = d(Replace(arr(i, 3), " ", ""))
- s = fn
- If Not d.exists(s) Then
- n = n + 1
- d(s) = n
- brr(1, n) = s
- End If
- c = d(fn)
- brr(r, c) = brr(r, c) + arr(i, c1)
- End If
- Next
- Next
- With sh
- bt = 3
- .UsedRange.Offset(2).Clear
- .Cells(bt, 1).Resize(m, 16) = brr
- .Cells(bt, 3).Resize(1, 13).Interior.Color = 49407
- .Cells(bt, 2).Resize(m, 1).Interior.Color = 5296274
- r = .Cells(Rows.Count, 1).End(3).Row
- .Cells(r + 1, 2) = "月份合计"
- With .Cells(bt, 1).Resize(m + 1, 16)
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- For i = bt + 1 To r
- .Cells(i, 16) = Application.Sum(.Cells(i, 3).Resize(, 13))
- Next
- .Cells(bt, 16).Resize(m + 1).Interior.ColorIndex = 8
- For j = 3 To 16
- .Cells(r + 1, j) = Application.Sum(.Cells(bt + 1, j).Resize(m - 1))
- .Cells(r + 1, 2).Resize(, 15).Interior.ColorIndex = 15
- Next
- End With
- Set d = Nothing
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
|
|