Excel:vba实现合并工作簿中的表

A、B、C这三个工作簿的数据都在sheet1,表头一样

Sub MergeWorkbooks()
    Dim FolderPath As String
    Dim FileName As String
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim mainWb As Workbook
    Dim mainWs As Worksheet
    Dim lastRow As Long
    Dim lastcol As Long
    
    Dim pasteRange As Range

    ' 主工作簿设置为当前工作簿
    Set mainWb = ThisWorkbook
    Set mainWs = mainWb.Sheets(1) ' 假设数据合并到第一张表中
    
    mainWs.Cells.Clear

    ' 获取文件夹路径(你可以根据需求修改文件夹路径)
    'FolderPath = "D:\VBA\hebin\" ' 更改为你实际存储文件的路径
    FolderPath = ThisWorkbook.Path & "\"

    ' 确保路径以反斜杠结尾
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    ' 获取第一个Excel文件
    FileName = Dir(FolderPath & "*.xlsx")
    
    ' 如果找不到任何文件,则提示并退出
    If FileName = "" Then
        MsgBox "未找到任何Excel文件,请检查路径或文件格式。"
        Exit Sub
    End If

    ' 循环所有Excel文件
    Do While FileName <> mainWb.Name
        ' 打开工作簿
        On Error Resume Next
        Set wb = Workbooks.Open(FolderPath & FileName)
        If Err.Number <> 0 Then
            MsgBox "无法打开文件:" & FileName
            Err.Clear
            Exit Sub
        End If
        On Error GoTo 0
        
        ' 假设数据在每个工作簿的第一张表中,找到最后一行并复制数据
        Set ws = wb.Sheets(1)
        lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        lastcol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        
        ws.Cells(1, 1).Resize(1, lastcol).Copy Destination:=mainWs.Cells(1, 1)
        
        ' 查找主工作簿中当前的最后一行
        If Application.WorksheetFunction.CountA(mainWs.Cells) > 0 Then
            Set pasteRange = mainWs.Cells(mainWs.Rows.Count, 1).End(xlUp).Offset(1, 0)
        Else
            Set pasteRange = mainWs.Cells(1, 1)
        End If

        ' 复制工作簿中的数据并粘贴到主工作簿
        'ws.Range("A1:" & ws.Cells(lastcol, lastRow).Address).Copy
        ws.Range("A2:E" & lastRow).Copy
        mainWs.Paste Destination:=pasteRange

        ' 关闭工作簿(不保存)
        wb.Close False

        ' 获取下一个文件
        FileName = Dir
    Loop
    
    With mainWs.Cells
        .HorizontalAlignment = xlCenter '设置水平居中
        .VerticalAlignment = xlCenter '设置垂直居中
        .Font.Size = 14
    End With

    ' 完成后提示
    MsgBox "所有工作簿已成功合并!"
End Sub

循环获取文件夹中的每个文件

Sub ListFiles()
    Dim fileName As String
    ' 第一次调用 Dir 并传入路径,获取第一个文件
    fileName = Dir(ThisWorkbook.Path & "/")
    
    ' 使用循环,逐步获取下一个文件
    Do While fileName <> ""
        MsgBox fileName   ' 显示文件名
        fileName = Dir    ' 不带参数,获取下一个文件
    Loop
End Sub
'如果想要获取路径,就Thisworkbook.Path & "/" & filename 
相关推荐
云只上6 小时前
前端插件使用xlsx-populate,花样配置excel内容,根据坐添加标替换excel内容,修改颜色,合并单元格...。
excel
小哥山水之间8 小时前
在 Python 中操作 Excel 文件
开发语言·python·excel
blog_wanghao9 小时前
C#: 创建Excel文件并在Excel中写入数据库中的数据
数据库·c#·excel
游客52016 小时前
自动化办公|通过xlwings进行excel格式设置
python·自动化·excel
喝西瓜汁的兔叽Yan16 小时前
第三方库XLSX: 前端上传excel文件,并对excel文件内容做校验。
前端·excel·xlsx
刘_sy1 天前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
PowerBI学谦2 天前
Copilot:Excel中的Python高级分析来了
python·excel·copilot
E-iceblue3 天前
Python 合并 Excel 单元格
python·excel·合并
sszdzq3 天前
Excel 合并列数据
excel
prince_zxill3 天前
Excel中不用复杂公式根据指定X列的数值N复制整行数据N行简单方法
excel