|
发表于 2017-6-6 14:54
|
显示全部楼层
本楼为最佳答案
- Sub aaa()
- Dim Arr, myPath$, myName$, Arr1, i&, j&, x$, y$
- Dim crr2(1 To 10000, 1 To 3)
- Dim d, d1, bt, k, t, k1, aa, s$
- Dim bb, m&, col&
- Set d = CreateObject("Scripting.Dictionary")
- Set d1 = CreateObject("Scripting.Dictionary")
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- Sheet1.Activate
- bt = Array("工作时间", "加班时间")
- [a3:e5000].Clear
- [f1:bz5002].Clear
- Cells.Font.Size = 11
- myPath = ThisWorkbook.Path & ""
- myName = Dir(myPath & "*.xls")
- Do While myName <> ""
- If InStr(myName, "汇总") = 0 Then
- With GetObject(myPath & myName)
- Arr1 = .Sheets(1).[a1].CurrentRegion
- For i = 3 To UBound(Arr1)
- x = Arr1(i, 2)
- y = Split(myName, ".")(0)
- If d.exists(x) = False Then
- Set d(x) = CreateObject("Scripting.Dictionary")
- nn = nn + 1
- crr2(nn, 1) = Arr1(i, 3)
- crr2(nn, 2) = Arr1(i, 4)
- crr2(nn, 3) = Arr1(i, 5)
- End If
- d(x)(y) = d(x)(y) & Arr1(i, 6) & "|" & Arr1(i, 7)
- d1(y) = ""
- Next
- .Close False
- End With
- End If
- myName = Dir
- Loop
- k = d.keys: t = d.items: k1 = d1.keys
- For i = 0 To UBound(k1) + 1
- If i <> UBound(k1) + 1 Then s = k1(i) Else s = "合计"
- With Cells(1, 2 * i + 6).Resize(1, 2)
- .Value = s
- .Merge
- .HorizontalAlignment = -4108
- .VerticalAlignment = -4108
- End With
- Cells(2, 2 * i + 6).Resize(1, 2) = bt
- Next
- col = 2 * d1.Count + 6
- [b3].Resize(d.Count) = Application.Transpose(k)
- crr = [b3].Resize(d.Count)
- [c3].Resize(nn, 3) = crr2
- [a3] = 1: [a4] = 2: [a3:a4].AutoFill [a3].Resize(d.Count)
- m = d.Count + 3
- Cells(m, 2) = "合计"
- For i = 0 To UBound(k)
- For j = 0 To UBound(k1)
- If d(k(i)).exists(k1(j)) Then
- bb = d(k(i))(k1(j))
- aa = Split(bb, "|")
- Cells(i + 3, 2 * j + 6) = Val(aa(0))
- Cells(i + 3, 2 * j + 7) = Val(aa(1))
- Cells(i + 3, col) = Cells(i + 3, col) + Val(aa(0))
- Cells(i + 3, col + 1) = Cells(i + 3, col + 1) + Val(aa(1))
- End If
- Next
- Next
- For i2 = 6 To col + 1
- Cells(m, i2) = WorksheetFunction.Sum(Range(Cells(3, i2).Address, Cells(m - 1, i2).Address))
- Next
- [a1].CurrentRegion.Borders.LineStyle = 1
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- End Sub
复制代码
偷懒,在你代码基础上稍微加了点
实现了你的要求
|
|