请Excel VBA高手帮忙---多工作簿数据汇总

2024-10-31 05:24:52
推荐回答(2个)
回答1:

在同目录新建一个工作薄,并打开ALT+F11进入VBA编辑,在左侧列表点击鼠标右键选插入一个模块,粘贴下面的代码
Sub 合并工作薄()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim MyFile As String, EndrowHZ, EndcolHZ, EndRow, EndCol As Long
Dim MyRng As Range
ActiveSheet.Name = "汇总"
EndrowHZ = Sheets("汇总").[A65536].End(xlUp).Row
MyFile = Dir(ActiveWorkbook.Path & "\*.xls")
If MyFile <> "" Then
Do
On Error Resume Next
If MyFile = ThisWorkbook.Name Then MyFile = Dir
Workbooks.Open (ActiveWorkbook.Path & "\" & MyFile)
Workbooks(MyFile).Sheets(1).Activate
EndRow = Workbooks(MyFile).Sheets(1).[A65536].End(xlUp).Row
If EndrowHZ = 1 Then Workbooks(ThisWorkbook.Name).Sheets("汇总").Rows(1).Value = Workbooks(MyFile).Sheets(1).Rows(1).Value
For i = 2 To EndRow
EndrowHZ = EndrowHZ + 1
Workbooks(ThisWorkbook.Name).Sheets("汇总").Rows(EndrowHZ).Value = Workbooks(MyFile).Sheets(1).Rows(i).Value
Next i
ActiveWorkbook.Close
MyFile = Dir
Loop While MyFile <> "" And MyFile <> ThisWorkbook.Name
End If
Workbooks(ThisWorkbook.Name).Sheets("汇总").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

回答2:

楼上的说的是SQL结合数据透视表...相当强大