簡單粗暴-將多個excel彙總到一個excel
阿新 • • 發佈:2020-11-18
首先將待合併的excel都放到一個檔案目錄下
新建一個excel右鍵sheet檢視程式碼 複製如下程式碼
Sub Macro1() Dim MyPath$, MyName$, sh As Worksheet, sht As Worksheet, m& Set sh = ActiveSheet MyPath = ThisWorkbook.Path & "\" MyName = Dir(MyPath & "*.xlsx") Application.ScreenUpdating = False Cells.ClearContents Do While MyName <> ""If MyName <> ThisWorkbook.Name Then With GetObject(MyPath & MyName) For Each sht In .Sheets If IsSheetEmpty = IsEmpty(sht.UsedRange) Then m = m + 1 If m = 1 Then sht.[a1].CurrentRegion.Copy sh.[a1] Else sht.[a1].CurrentRegion.Offset(1).Copy sh.[a65536].End(xlUp).Offset(1) '這種寫法只保留第一個檔案的表頭'sht.[a1].CurrentRegion.Copy sh.[a65536].End(xlUp).Offset(1) 這種會提取所有行資訊
'2007版及以後可以改成a1048576 但不建議,最好取多個檔案有值的最大行數
End If End If Next .Close False End With End If MyName = Dir Loop Application.ScreenUpdating = True MsgBox "當前工作簿下的全部工作表已經合併完畢!", vbInformation, "提示" End Sub
參考如下並整理
作者: 知乎使用者
連結:https://www.zhihu.com/question/20366713/answer/109112356
來源: 知乎