美文网首页
多个Excel表合并成一个表

多个Excel表合并成一个表

作者: 话_5262 | 来源:发表于2018-09-11 11:06 被阅读0次

    1、新建一文件夹
    2、把需要合并的Excel文件放入当前文件夹下
    3、在当前文件夹下新建一个Excel表
    4、打开文件 --> 右击Sheet1 --> 查看代码 --> 输入下方代码

    Sub 合并当前目录下所有工作簿的全部工作表()
    
    Dim MyPath, MyName, AWbName
    
    Dim Wb As Workbook, WbN As String
    
    Dim G As Long
    
    Dim Num As Long
    
    Dim BOX As String
    
    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
    
    .Cells(.Range("B65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
    
    For G = 1 To Sheets.Count
    
    Wb.Sheets(G).UsedRange.Copy .Cells(.Range("B65536").End(xlUp).Row + 1, 1)
    
    Next
    
    WbN = WbN & Chr(13) & Wb.Name
    
    Wb.Close False
    
    End With
    
    End If
    
    MyName = Dir
    
    Loop
    
    Range("B1").Select
    
    Application.ScreenUpdating = True
    
    MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
    
    End Sub
    
    
    5、运行 --> 运行子过程/用户窗体

    6、接下来就OK了~~~点击确定完成操作


    相关文章

      网友评论

          本文标题:多个Excel表合并成一个表

          本文链接:https://www.haomeiwen.com/subject/nzwygftx.html