Excel 多工作簿合并 VBA 脚本
中文 README
Excel 多工作簿合并 VBA 脚本
工具简介
该 VBA 宏用于将多个 Excel 工作簿的 第一个工作表,合并到一个全新的 Excel 工作簿中。新工作簿中的每个合并工作表,将以原工作簿的名称(不含文件后缀)命名。
无需手动复制粘贴,自动化完成多文件数据整合,提升工作效率。
功能特性
-
通过文件选择对话框,可批量选择多个 Excel 工作簿。
-
仅提取并复制每个选中工作簿的 第一个工作表。
-
新工作簿中的工作表名称,自动匹配原工作簿名称(去除 .xlsx 后缀)。
-
操作简单,无需复杂配置,一键运行。
前置条件
-
安装 Microsoft Excel(支持 Excel 2007 及以上版本)。
-
待合并的 Excel 文件需为 .xlsx 格式(若使用 .xls 或 .xlsm 格式,需修改代码中的文件后缀)。
-
Excel 需启用宏功能(运行 VBA 脚本的必要条件)。
使用方法
-
打开任意一个空白 Excel 工作簿。
-
按下
Alt + F11组合键,打开 VBA 编辑器。 -
在 VBA 编辑器中,右键点击“工程”面板中的工作簿名称 → 选择 插入 → 模块。
-
将提供的完整 VBA 代码复制粘贴到新建的模块窗口中。
-
按下
F5键运行宏,或点击 VBA 编辑器中的“运行”按钮(绿色三角形)。 -
弹出文件选择对话框,选中所有需要合并的 Excel 工作簿(按住 Ctrl 键可多选)。
-
等待宏运行完成,将自动生成一个新工作簿,包含所有选中文件的第一个工作表,工作表名称与原工作簿名称一致。
代码说明
-
代码默认去除 .xlsx 后缀来命名工作表。若你的文件是 .xls 格式(Excel 97-2003 版本),请将代码中的
VBA.Replace(tempwb.Name, ".xlsx", "")修改为VBA.Replace(tempwb.Name, ".xls", "")。 -
原工作簿会被临时打开,完成复制后自动关闭,且不会保存任何修改。
-
若原工作簿名称包含 Excel 工作表名称不允许的特殊字符(如 \ / ? * [ ] 等),Excel 会自动调整工作表名称,必要时可手动修改。
免责声明
本脚本免费提供,不提供任何形式的担保或承诺。为避免意外错误导致数据丢失,请在运行宏前,备份好所有原始文件。
'功能:把多个excel工作簿的第一个sheet工作表合并到一个excel工作簿的多个sheet工作表,新工作表的名称等于原工作簿的名称Sub Books2Sheets() '定义对话框变量 Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker)'新建一个工作簿 Dim newwb As Workbook Set newwb = Workbooks.Add With fd If .Show = -1 Then '定义单个文件变量 Dim vrtSelectedItem As Variant'定义循环变量 Dim i As Integer i = 1 '开始文件检索 For Each vrtSelectedItem In .SelectedItems'打开被合并工作簿 Dim tempwb As Workbook Set tempwb = Workbooks.Open(vrtSelectedItem) '复制工作表 tempwb.Worksheets(1).Copy Before:=newwb.Worksheets(i)'把新工作簿的工作表名字改成被复制工作簿文件名,这儿应用于xls文件,即Excel97-2003的文件,如果是Excel2007,需要改成xlsx newwb.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xlsx", "") '关闭被合并工作簿 tempwb.Close SaveChanges:=False i = i + 1 Next vrtSelectedItem End If End With Set fd = NothingEnd Sub
Excel 多工作簿合并 VBA 脚本 README
English README
Excel Workbook Merge VBA Script
Overview
This VBA macro is designed to merge the first worksheet of multiple Excel workbooks into a single new Excel workbook. Each merged worksheet in the new workbook will be named after the original workbook (excluding the file extension).
It provides a simple, automated way to consolidate data from multiple Excel files without manual copying and pasting, improving work efficiency.
Features
-
Automatically select multiple Excel workbooks via a file picker dialog.
-
Extract and copy only the first worksheet from each selected workbook.
-
Name the merged worksheets in the new workbook after the original workbook names (removing the .xlsx extension).
-
Simple operation, no complex configuration required.
Prerequisites
-
Microsoft Excel (supports Excel 2007 and later versions).
-
The Excel files to be merged must be in .xlsx format (modify the extension in the code if using .xls or .xlsm).
-
Enable macros in Excel (required to run VBA scripts).
How to Use
-
Open any blank Excel workbook.
-
Press
Alt + F11to open the VBA Editor. -
In the VBA Editor, right-click the workbook name in the “Project Explorer” panel → Select Insert → Module.
-
Copy the entire VBA code provided into the new module window.
-
Press
F5to run the macro, or click the “Run” button (green triangle) in the VBA Editor. -
A file picker dialog will pop up. Select all the Excel workbooks you want to merge (hold Ctrl to select multiple files).
-
Wait for the macro to finish running. A new workbook will be generated, containing the first worksheet of each selected file, with worksheet names matching the original workbook names.
Code Notes
-
The code currently uses
.xlsxas the file extension to remove from worksheet names. If your files are in .xls format (Excel 97-2003), modifyVBA.Replace(tempwb.Name, ".xlsx", "")toVBA.Replace(tempwb.Name, ".xls", ""). -
The original workbooks will be opened temporarily and closed automatically without saving any changes.
-
If the original workbook name contains special characters that are not allowed in Excel worksheet names (e.g., \ / ? * [ ]), the worksheet name will be automatically adjusted by Excel (you may need to rename it manually if necessary).
Disclaimer
This script is provided for free without any guarantees or warranties. Please back up your original files before running the macro to avoid data loss due to unexpected errors.
夜雨聆风