Sub MergeFiles()
Dim path As String, fileName As String, ws As Worksheet
Dim totalSheet As Worksheet, lastRow As Long, sourceLastRow As Long
Dim firstFile As Boolean
Set totalSheet = ThisWorkbook.Sheets("Sheet1") ' 修改为汇总表名称
path = "你的Excel文件夹路径\" ' 结尾需有反斜杠
fileName = Dir(path & "*.xls*")
firstFile = True
Do While fileName <> ""
Dim wb As Workbook
Set wb = Workbooks.Open(path & fileName)
Set ws = wb.Sheets(1) ' 假设数据在第一个工作表
' 获取数据范围
sourceLastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
If sourceLastRow < 10 Then GoTo NextFile ' 无数据跳过,此代码从第10行开始提取数据汇总
If firstFile Then
' 复制标题和数据(第10行及之后)
ws.Range("A10", ws.Cells(sourceLastRow, "ZZ")).Copy totalSheet.Range("A1")
firstFile = False
Else
' 仅复制数据(第11行及之后)
ws.Range("A11", ws.Cells(sourceLastRow, "ZZ")).Copy _
totalSheet.Cells(totalSheet.Rows.Count, 1).End(xlUp).Offset(1)
End If
NextFile:
wb.Close False
fileName = Dir
Loop
MsgBox "合并完成!"
End Sub