EXCEL VBA合并当前工作簿的所有工作表sheet

将当前工作簿 的所有工作表合并到到1个新的sheet,

新的sheet名称为 合并

分为2个vba脚本 ,

  1. 不包含表头: 每个sheet的表头都是相同的,所以合并时不需要表头
  2. 包含表头

VBA代码通过KIMI生成

1 不包含表头(标题行)

复制代码
Sub 合并所有工作表_不含表头()
    Dim ws As Worksheet, wsNew As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim destRow As Long
    Dim copyRange As Range
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    '如已存在"合并"工作表,则删除
    On Error Resume Next
    Set wsNew = ThisWorkbook.Worksheets("合并")
    If Not wsNew Is Nothing Then wsNew.Delete
    On Error GoTo 0
    
    '新建"合并"工作表
    Set wsNew = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    wsNew.Name = "合并"
    
    destRow = 1   '目标行指针
    
    '遍历所有工作表
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "合并" Then
            If Application.WorksheetFunction.CountA(ws.Cells) > 0 Then
                '=== 关键修复:用 Find 取真正的最后一行/列 ===
                lastRow = ws.Cells.Find(What:="*", _
                                        After:=ws.Cells(1, 1), _
                                        SearchOrder:=xlByRows, _
                                        SearchDirection:=xlPrevious).Row
                lastCol = ws.Cells.Find(What:="*", _
                                        After:=ws.Cells(1, 1), _
                                        SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlPrevious).Column
                
                '标题行:只在第一张工作表出现时复制
                If destRow = 1 Then
                    wsNew.Cells(destRow, 1).Value = "来源工作表"
                    ws.Range(ws.Cells(1, 1), ws.Cells(1, lastCol)).Copy _
                        Destination:=wsNew.Cells(destRow, 2)
                    destRow = destRow + 1
                End If
                
                '复制数据区(不含标题)
                Set copyRange = ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, lastCol))
                copyRange.Copy wsNew.Cells(destRow, 2)
                
                '在A列写入来源工作表名称
                wsNew.Range(wsNew.Cells(destRow, 1), _
                            wsNew.Cells(destRow + copyRange.Rows.Count - 1, 1)).Value = ws.Name
                
                '移动目标行指针
                destRow = destRow + copyRange.Rows.Count
            End If
        End If
    Next ws
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "已完成合并,请查看"合并"工作表!", vbInformation
End Sub

2 包含表头(标题行)

复制代码
Sub 合并所有工作表_含表头()
    Dim ws As Worksheet, wsNew As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim destRow As Long
    Dim copyRange As Range
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    '如已存在"合并"工作表,则删除
    On Error Resume Next
    Set wsNew = ThisWorkbook.Worksheets("合并")
    If Not wsNew Is Nothing Then wsNew.Delete
    On Error GoTo 0
    
    '新建"合并"工作表
    Set wsNew = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    wsNew.Name = "合并"
    
    destRow = 1   '目标行指针
    
    '遍历所有工作表
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "合并" Then
            If Application.WorksheetFunction.CountA(ws.Cells) > 0 Then
                '=== 用 Find 取真正的最后一行/列 ===
                lastRow = ws.Cells.Find(What:="*", _
                                        After:=ws.Cells(1, 1), _
                                        SearchOrder:=xlByRows, _
                                        SearchDirection:=xlPrevious).Row
                lastCol = ws.Cells.Find(What:="*", _
                                        After:=ws.Cells(1, 1), _
                                        SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlPrevious).Column
                
                '复制当前工作表全部内容(含表头)
                Set copyRange = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
                copyRange.Copy wsNew.Cells(destRow, 2)   '从 B 列开始粘贴
                
                '在 A 列写入来源工作表名称
                wsNew.Range(wsNew.Cells(destRow, 1), _
                            wsNew.Cells(destRow + copyRange.Rows.Count - 1, 1)).Value = ws.Name
                
                '移动目标行指针
                destRow = destRow + copyRange.Rows.Count
            End If
        End If
    Next ws
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "已完成合并(含表头),请查看"合并"工作表!", vbInformation
End Sub
相关推荐
_大龄1 天前
前端解析excel
前端·excel
johnny2332 天前
智能电子表格:Airtable、NocoDB、teable、APITable
excel
2501_930707782 天前
如何使用C#代码在Excel 文件中添加工作表
excel
shouchaobao2 天前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
allbs3 天前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
m5655bj3 天前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
温轻舟3 天前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
WarPigs3 天前
Unity编辑器开发笔记
unity·编辑器·excel
allbs3 天前
spring boot项目excel导出功能封装——3.图表导出
spring boot·后端·excel
lqz19934 天前
根据html导出excel和word
html·word·excel