|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test2()
- Dim r%, i%
- Dim arr, brr, bt()
- Dim ws As Worksheet
- Dim d As Object
- Set d = CreateObject("scripting.dictionary")
-
- n = 0
- For Each ws In Worksheets
- If ws.Name <> "汇总" Then
- n = n + 1
- ReDim Preserve bt(1 To n)
- bt(n) = ws.Name
- End If
- Next
-
- For k = 1 To UBound(bt)
- With Worksheets(bt(k))
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- c = .Cells(1, .Columns.Count).End(xlToLeft).Column
- arr = .Range("a1").Resize(r, c)
- For j = 1 To UBound(arr, 2)
- If arr(1, j) = "应发合计" Then
- Exit For
- End If
- Next
- If j <= UBound(arr, 2) Then
- For i = 2 To UBound(arr)
- If Not d.exists(arr(i, 2)) Then
- ReDim brr(1 To UBound(bt) + 2)
- brr(1) = arr(i, 2)
- Else
- brr = d(arr(i, 2))
- End If
- brr(k + 1) = brr(k + 1) + arr(i, j)
- brr(UBound(bt) + 2) = brr(UBound(bt) + 2) + arr(i, j)
- d(arr(i, 2)) = brr
- Next
- End If
- End With
- Next
- brr = Application.Transpose(Application.Transpose(d.items))
- ReDim crr(1 To UBound(brr, 2))
- crr(1) = "合计"
- For j = 2 To UBound(brr, 2)
- crr(j) = Application.Sum(Application.Index(brr, 0, j))
- Next
-
-
- With Worksheets("汇总")
- .Cells.Clear
- .Range("a1") = "姓名"
- .Range("b1").Resize(1, UBound(bt)) = bt
- .Cells(1, UBound(bt) + 2) = "合计"
- .Range("a2").Resize(UBound(brr), UBound(brr, 2)) = brr
- r = UBound(brr) + 2
- .Cells(r, 1).Resize(1, UBound(crr)) = crr
- .Range("a1").Resize(r, UBound(bt) + 2).Borders.LineStyle = xlContinuous
- With .Range("a:a,1:1")
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
-
- End With
-
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|