|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
原帖由 商人 于 2011-6-26 11:54 发表
谢谢赵刚老师的指导!同表格里的我学会了,但是我还想加点难度的,将单独只是汇总单一工作表格的,改为:多工作簿,同格式不同标题汇总。
效果见下图!文件见附件!
老师的代码相当简洁,汇总多工作簿的, ...
你的数据源不太规范,否则可以用SQL语句导入,连透视表都可以省略
下面代码是根据上面一个工作簿代码修改的,请测试:
Sub Macro1()
Dim arrpath$(), arr, brr(), sh As Worksheet, i&, j&, m&, n&, k&, d As Object
Dim myPath$, myFile$, wb As Workbook, s$, w$
Set d = CreateObject("scripting.dictionary")
d("工作簿") = 1
d("工作表") = 2
m = 2
Set wb1 = ThisWorkbook
Application.ScreenUpdating = False
myPath = ThisWorkbook.Path & "\数据源\"
myFile = Dir(myPath & "*.xls")
Do While myFile <> ""
n = n + 1 '工作簿计数
ReDim Preserve arrpath(1 To n) '重新定义工作簿路径数组
arrpath(n) = myPath & myFile '记录工作簿路径
Set wb = GetObject(arrpath(n)) '调用这个工作簿
For Each sh In wb.Sheets
With sh
If IsSheetEmpty = IsEmpty(.UsedRange) Then
arr = .[b3].Resize(.[b65536].End(xlUp).Row - 2, .[iv3].End(xlToLeft).Column - 1)
For j = 1 To UBound(arr, 2)
If Not d.Exists(arr(1, j)) Then
m = m + 1
d(arr(1, j)) = m
End If
Next
End If
End With
Next
wb.Close False
myFile = Dir
Loop
ReDim brr(1 To 60000, 1 To d.Count)
m = 0
For k = 1 To n '逐个工作簿
w = Split(Split(arrpath(k), "\")(UBound(Split(arrpath(k), "\"))), ".")(0)
Set wb = GetObject(arrpath(k)) '调用工作簿
For Each sh In wb.Sheets
With sh
If IsSheetEmpty = IsEmpty(.UsedRange) Then
s = .Name
arr = .[b3].Resize(.[b65536].End(xlUp).Row - 2, .[iv3].End(xlToLeft).Column - 1)
For i = 2 To UBound(arr)
m = m + 1
brr(m, 1) = w
brr(m, 2) = s
For j = 1 To UBound(arr, 2)
brr(m, d(arr(1, j))) = arr(i, j)
Next
Next
End If
End With
Next
wb.Close False
Next
ActiveSheet.UsedRange.ClearContents
[a1].Resize(, d.Count) = d.Keys
[a2].Resize(m, d.Count) = brr
Application.ScreenUpdating = True
MsgBox "汇总完毕"
End Sub
[ 本帖最后由 zhaogang1960 于 2011-6-26 13:28 编辑 ] |
|