ARTICLE · 1500750
Excel几百个表格的内容合并到一个工作表中,并且把表格的名称也提取过来,DeepSeek助力一键完成,最好用的方法,没有之一









Dim fd As FileDialog
Dim selectedFiles() As String
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim wsSummary As Worksheet
Dim dictHeaders As Object
Dim headerOrder As Collection
Dim destRow As Long
Dim startTime As Double
Dim i As Long, fileCount As Long
startTime = Timer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' 初始化数据结构
Set dictHeaders = CreateObject("Scripting.Dictionary")
Set headerOrder = New Collection
dictHeaders.CompareMode = vbTextCompare
' 添加固定系统列
dictHeaders.Add "|SYS_来源工作簿|", 1
headerOrder.Add "|SYS_来源工作簿|"
dictHeaders.Add "|SYS_来源工作表|", 2
headerOrder.Add "|SYS_来源工作表|"
' 文件选择对话框
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "选择要合并的Excel文件"
.AllowMultiSelect = True
.Filters.Clear
.Filters.Add "Excel文件", "*.xls;*.xlsx;*.xlsm"
If .Show Then
fileCount = .SelectedItems.Count
If fileCount = 0 Then Exit Sub
ReDim selectedFiles(1 To fileCount)
For i = 1 To fileCount
selectedFiles(i) = .SelectedItems(i)
Next i
Else
Exit Sub
End If
End With
' 第一次遍历:收集所有唯一标题
For i = 1 To fileCount
On Error Resume Next
Set wbSource = Workbooks.Open(selectedFiles(i), True, True)
If Err.Number <> 0 Then
MsgBox "无法打开文件:" & selectedFiles(i), vbExclamation
Err.Clear
GoTo NextFile
End If
On Error GoTo 0
For Each wsSource In wbSource.Worksheets
If wsSource.Visible = xlSheetVisible Then
CollectHeaders wsSource, dictHeaders, headerOrder
End If
Next wsSource
wbSource.Close False
NextFile:
Next i
' 创建汇总表
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("汇总").Delete
On Error GoTo 0
Set wsSummary = ThisWorkbook.Sheets.Add(Before:=Sheets(1))
wsSummary.Name = "汇总"
Application.DisplayAlerts = True
' 写入标准化表头
Dim headers() As String
ReDim headers(1 To headerOrder.Count)
For i = 1 To headerOrder.Count
headers(i) = Replace(headerOrder(i), "|SYS_", "")
Next
wsSummary.Range("A1").Resize(1, UBound(headers)) = headers
' 第二次遍历:合并数据
destRow = 2
For i = 1 To fileCount
On Error Resume Next
Set wbSource = Workbooks.Open(selectedFiles(i), True, True)
If Err.Number <> 0 Then
Err.Clear
GoTo NextProcess
End If
On Error GoTo 0
For Each wsSource In wbSource.Worksheets
If wsSource.Visible = xlSheetVisible Then
MergeSheetData wsSource, wsSummary, dictHeaders, destRow, _
GetFileName(selectedFiles(i))
End If
Next wsSource
wbSource.Close False
NextProcess:
Next i
' 格式处理
With wsSummary
.UsedRange.EntireColumn.AutoFit
.Rows(1).Font.Bold = True
.Rows(1).Interior.Color = RGB(198, 224, 180)
.Columns("A:B").HorizontalAlignment = xlCenter
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "合并完成!" & vbCrLf & _
"处理文件数:" & fileCount & vbCrLf & _
"总数据行数:" & destRow - 2 & vbCrLf & _
"耗时:" & Format(Timer - startTime, "0.00") & " 秒", _
vbInformation, "报告"
End Sub
' 获取文件名(不含路径)
Private Function GetFileName(fullPath As String) As String
GetFileName = Mid(fullPath, InStrRev(fullPath, "\") + 1)
End Function
' 智能标题收集
Private Sub CollectHeaders(ws As Worksheet, ByRef dict As Object, ByRef colOrder As Collection)
Dim lastCol As Long
Dim cell As Range
Dim cleanKey As String
With ws
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For Each cell In .Range(.Cells(1, 1), .Cells(1, lastCol))
cleanKey = NormalizeHeader(cell.Value)
If cleanKey <> "" Then
If Not dict.Exists(cleanKey) Then
dict.Add cleanKey, dict.Count + 1
colOrder.Add cleanKey
End If
End If
Next cell
End With
End Sub
' 标题标准化处理
Private Function NormalizeHeader(rawText As String) As String
Dim result As String
' 基础清洗
result = Trim(Replace(rawText, Chr(10), "")) ' 去换行符
result = Replace(result, Chr(13), "") ' 去回车符
result = Replace(result, " ", "_") ' 替换空格
' 排除系统列
If result = "来源工作簿" Or result = "来源工作表" Then
NormalizeHeader = ""
Exit Function
End If
' 长度控制
result = Left(result, 50)
' 转换为小写去重
NormalizeHeader = LCase(result)
End Function
' 数据合并核心
Private Sub MergeSheetData(wsSrc As Worksheet, wsSum As Worksheet, dict As Object, _
ByRef destRow As Long, wbName As String)
Dim lastRow As Long, lastCol As Long
Dim headerMap As Object
Dim dataArr() As Variant
Dim sumArr() As Variant
Dim i As Long, j As Long, colIndex As Long
Set headerMap = CreateObject("Scripting.Dictionary")
' 建立列映射
With wsSrc
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For j = 1 To lastCol
Dim srcHeader As String
srcHeader = NormalizeHeader(.Cells(1, j).Value)
If srcHeader <> "" Then
If dict.Exists(srcHeader) Then
headerMap(j) = dict(srcHeader)
End If
End If
Next j
End With
' 读取数据
With wsSrc
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
If lastRow < 2 Then Exit Sub
dataArr = .Range("A2", .Cells(lastRow, lastCol)).Value
End With
' 构建输出数组
ReDim sumArr(1 To UBound(dataArr, 1), 1 To dict.Count)
For i = 1 To UBound(dataArr, 1)
' 系统列赋值
sumArr(i, 1) = wbName
sumArr(i, 2) = wsSrc.Name
Next i
' 批量写入
If UBound(sumArr, 1) > 0 Then
wsSum.Cells(destRow, 1).Resize(UBound(sumArr, 1), dict.Count) = sumArr
destRow = destRow + UBound(sumArr, 1)
End If
End Sub















































































