如下的 VBA 代码可以帮助我们将一个Excel文件中的多个 Sheet 合并到一起:
Sub Merge_Sheets()
'Insert a new worksheet
Sheets.Add
'Rename the new worksheet
ActiveSheet.Name = "ProfEx_Merged_Sheet"
'Loop through worksheets and copy the to your new worksheet
For Each ws In Worksheets
ws.Activate
'Don't copy the merged sheet again
If ws.Name <> "ProfEx_Merged_Sheet" Then
ws.UsedRange.Select
Selection.Copy
Sheets("ProfEx_Merged_Sheet").Activate
'Select the last filled cell
ActiveSheet.Range("A1048576").Select
Selection.End(xlUp).Select
'For the first worksheet you don't need to go down one cell
If ActiveCell.Address <> "$A$1" Then
ActiveCell.Offset(1, 0).Select
End If
'Instead of just paste, you can also paste as link, as values etc.
ActiveSheet.Paste
End If
Next
End Sub
来源:
Merge Sheets: Easily Copy Excel Sheets Underneath on One Sheet!