當資料來源是多個Excel
但卻想做一個樞扭分析表
冏了,不會要一個一個打開吧!
於是,要合併Excel或是合併多個Sheet資料如下:
'合併多個Excel檔案
Sub GetSheets()
Path = "檔案資料夾"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
'只複製第一個Sheet
If ActiveWorkbook.Sheets.Count > 0 Then
ActiveWorkbook.Sheets(1).Copy _
After:=ThisWorkbook.Sheets(1)
'每個Sheet都複製
'For Each Sheet In ActiveWorkbook.Sheets
'Sheet.Copy After:=ThisWorkbook.Sheets(1)
'Next Sheet
End If
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
合併多個工作表(Sheet)
Sub Combine()
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub
參考資料