vba實現excel多表合併

Excel多表合併之vba實現oop

需求

保留列名,複製每個excel裏的數據,合併到一個excelspa

操做步驟

  1. 將要合併的文件放在同一文件夾下,複製過來就好(ps:最好不要直接操做原數據文件,避免操做失敗,數據丟失)
  2. 在這個目錄下建立一個「合併.xlsx」
  3. 雙擊打開「合併.xlsx」
  4. 同時按 ALT + F11
  5. 出現下圖,按圖中文字操做便可完成合並

 

  1. 完成

 

 

 

 

附錄代碼

Sub 合併當前目錄下全部工做簿的所有工做表()
    Dim MyPath, MyName, AWbName
    Dim Wb As Workbook, WbN As String
    Dim G As Long
    Dim Num As Long
    Dim BOX As String
    flag = 0
    
    Application.ScreenUpdating = False
    MyPath = ActiveWorkbook.Path
    MyName = Dir(MyPath & "\" & "*.xls")
    AWbName = ActiveWorkbook.Name
    Num = 0
    
    Do While MyName <> ""
        If MyName <> AWbName Then
            Set Wb = Workbooks.Open(MyPath & "\" & MyName)
            Num = Num + 1
            With Workbooks(1).ActiveSheet
                For G = 1 To Sheets.Count
                    If flag = 0 Then
                        Wb.Sheets(G).UsedRange.Copy .Cells(.Range("A65536").End(xlUp).Row , 1)
                        flag = 1
                    Else
                        Wb.Sheets(G).Range("a2", Wb.Sheets(G).Cells.SpecialCells(xlCellTypeLastCell)).Copy .Cells(.Range("A65536").End(xlUp).Row + 1, 1)

                    End If
                Next
                WbN = WbN & Chr(13) & Wb.Name
                Wb.Close False
            End With
        End If
        MyName = Dir
    Loop
        Range("A1").Select
        
        
    Application.ScreenUpdating = True
    MsgBox "共合併了" & Num & "個工做薄下的所有工做表。以下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub

 合併多個excel每一個excel有多個sheet,每一個sheet單獨合併,代碼以下excel

Sub 合併當前目錄下全部工做簿的所有工做表()
    Dim MyPath, MyName, AWbName
    Dim Wb As Workbook, WbN As String
    Dim G As Long
    Dim Num As Long
    Dim BOX As String
    flag = 0
    
    Application.ScreenUpdating = False
    MyPath = ActiveWorkbook.Path
    MyName = Dir(MyPath & "\" & "*.xls")
    AWbName = ActiveWorkbook.Name
    Num = 0
 
    
    Do While MyName <> ""
        If MyName <> AWbName Then
            Set Wb = Workbooks.Open(MyPath & "\" & MyName)
            Num = Num + 1
            
            
                For G = 1 To Wb.Sheets.Count
                    
                    If flag = 0 Then
                    Sheets.Add after:=Sheets(Sheets.Count)
                    
                        With ActiveSheet
                               .Name = Wb.Sheets(G).Name
                           Wb.Sheets(G).UsedRange.Copy .Cells(.Range("A65536").End(xlUp).Row, 1)
                           .UsedRange.Rows.AutoFit
                           .UsedRange.Columns.AutoFit
                        End With
                    Else
                          With Workbooks(1).Worksheets(G + 3)
                          ' MsgBox .Name & "--" & Wb.Sheets(G).Name
                           If G = 2 Then
                            Wb.Sheets(G).Range("a2", Wb.Sheets(G).Cells.SpecialCells(xlCellTypeLastCell)).Copy .Cells(.Range("A65536").End(xlUp).Row + 2, 1)
                           Else
                            Wb.Sheets(G).Range("a2", Wb.Sheets(G).Cells.SpecialCells(xlCellTypeLastCell)).Copy .Cells(.Range("A65536").End(xlUp).Row + 1, 1)
                            End If
                            
                           .UsedRange.Rows.AutoFit
                           .UsedRange.Columns.AutoFit
                          End With
                          
                    End If
                Next
                'flag 爲0時候爲第一個打開的excel,此時產生列,sheet名
                 flag = 1
                WbN = WbN & Chr(13) & Wb.Name
                Wb.Close False
           ' End With
        End If
        MyName = Dir
    Loop
        Range("A1").Select
        
        
    Application.ScreenUpdating = True
    MsgBox "共合併了" & Num & "個工做薄下的所有工做表。以下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
相關文章
相關標籤/搜索