• vba实现excel多表合并,第二个表有错误


    Excel多表合并之vba实现

    需求

    保留列名,复制每一个excel里的数据,合并到一个excel

    操作步骤

    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单独合并,代码如下      

     Sub 合并当前目录下所有工作簿的全部工作表()
        Dim MyPath, MyName, AWbName
        Dim Wb As Workbook, WbN As String
        Dim 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
  • 相关阅读:
    Python爬虫入门教程 59-100 python爬虫高级技术之验证码篇5-极验证识别技术之二
    CouchDB简介
    零成本打造抖音IP,轻松实现月入过万,90%的人都不懂
    couchdb集群搭建
    汽车测评详细操作流程,一篇赚300+
    基于docker部署的微服务架构: docker环境下的zookeeper和kafka部署
    零成本的互联网赚钱项目,都是怎么做的?
    SQuirrel连接hive配置
    本人有8万启动资金,做点什么生意好呢?
    PHP实现自己活了多少岁
  • 原文地址:https://www.cnblogs.com/medik/p/11094131.html
Copyright © 2020-2023  润新知