|
- Sub 汇总本夹内多簿多表数据() '
- Dim Mypath$, Myname$
- Dim sh As Worksheet, rng As Range
- Dim arr, brr(), i&, j%, n&, r&
- Mypath = ThisWorkbook.Path & "" '本文件夹名
- Myname = Dir(Mypath & "*.xlsx") '本文件夹下的工作簿
- Application.ScreenUpdating = False
- ReDim brr(1 To 5000, 1 To 12) '定义动态数组大小
- Do While Myname <> "" '遍历工作簿查找
- If Myname <> ThisWorkbook.Name Then '如果工作簿不是本工作簿
- With GetObject(Mypath & Myname) '后台打开该工作簿
- For Each sh In .Sheets '打开的工作簿再遍历工作表查找
- Set rng = sh.Cells(3, 1) '工作表的A3单元格
- If Not rng Is Nothing Then '如果A3单元格不为空
- r = sh.Cells(65536, 1).End(3).Row '返回该工作表的最下标定义为r
- arr = sh.Range("a3:l" & r).Value '把数据区域赋值给数组arr
- For i = 1 To UBound(arr) 'arr起始循环
- If arr(i, 1) <> "" Then '如果有值
- n = n + 1 '逐行递增
- For j = 1 To 12 '第一列到12列循环
- brr(n, j) = arr(i, j) '把arr数组区的值赋给数组brr
- Next
- End If
- Next
- End If
- Next
- .Close False
- End With
- End If
- Myname = Dir
- Loop
- With Sheets("汇总表")
- Range("a3:l" & Rows.Count).Clear '先清除a3以下所有数据及格式
- Range("a3").Resize(n, 12) = brr '再把brr数组赋值到A3开始以下区域
- Cells(n + 3, 1) = "合 计"
- For j = 3 To 12
- Cells(n + 3, j).Value = Application.WorksheetFunction.Sum(Range(Cells(3, j), Cells(n + 2, j)))
- Next
- With Range("a3").Resize(n + 1, 12)
- .Resize(n + 1, 12).Borders.LineStyle = 1 '添加边框线
- .Font.Size = 10 '字体为10号
- End With
- [a1].Resize(n + 3, 12).BorderAround xlContinuous, xlMedium '外边框加粗
- [a3].Resize(n + 1, 1).HorizontalAlignment = xlCenter ' xlLeft '向左对齐 xlCenter '居中对齐 xlRight '向右对齐
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|