excel 合并工作表的方法,用VBA代码实现合并工作表
时间:2023-08-06 14:03:45
下图所示:一个文件夹下面有多个excel工作薄,每个工作薄文件下面有不固定张数的工作表。比如有的文件只有一张工作,有的文件是多张工作表。
现在的问题,如何将这些多个文件实现合并工作表。将这些所有工作表全部合并到同一张工作表。
一般的做法,通过“移动或复制”实现合并工作表。要高效,可以使用VBA代码来实现合并工作表。
合并工作表操作步骤:
新建一个excel文件,按ALT+F11,打开VBE编辑器,复制下面的代码,然后运行,选择文件夹下面的所有excel文件,执行合并工作表。
Sub 合并工作簿()
Dim FilesToOpen, wb As Workbook, sht As Worksheet
Dim x As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xlsx), *.xlsx", _
MultiSelect:=True, Title:="Files to Merge")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
While x <= UBound(FilesToOpen)
If FilesToOpen(x) <> ThisWorkbook.FullName Then
Set wb = Workbooks.Open(Filename:=FilesToOpen(x))
For Each sht In wb.Sheets
sht.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Next
wb.Close
End If
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
说明:上面代码是合并xlsx文件,如果是合并excel2003的文件,需要将:FileFilter:="Microsoft Excel Files (*.xlsx), *.xlsx",xlsx改为.xls。
excel 合并工作表的方法,用VBA代码实现合并工作表的下载地址:
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win10 Mobile预览版10512最新功能 Gif照片已支持512MB内存机型
![](https://img.aspxhome.com/file/2023/28/a248224_0s.jpg)
如何将多张图片批量转成 PDF
![](https://img.aspxhome.com/file/2023/9/a342597_0s.jpg)
Win10蓝屏错误代码critical_structure_corruption解决方法
![](https://img.aspxhome.com/file/2023/5/a314889_0s.jpg)
掌握 Word 中的两个高效操作技巧,按时下班靠自己
![](https://img.aspxhome.com/file/2023/5/17115_0s.png)
Win7系统IE报错“已停止工作”的解决方法
![](https://img.aspxhome.com/file/2023/4/a303848_0s.jpg)
研究显示 Apple Watch可准确判断用户的身体“脆弱程度”
![](https://img.aspxhome.com/file/2023/7/a207707_0s.jpeg)
NVIDIA送出更贵GTX 1080:11Gbps显存
![](https://img.aspxhome.com/file/2023/4/a307806_0s.jpg)
WPS表格中怎样装裱字画
PS新手教程-如何使用PS快速给一寸证件照片进行排版
![](https://img.aspxhome.com/file/2023/10/a344107_0s.jpeg)
如何快速让PPT图表动起来
ppt怎么简笔画效果的柚子? ppt柚子的画法
![](https://img.aspxhome.com/file/2023/10/a345494_0s.png)
win10通知栏快捷键是什么
![](https://img.aspxhome.com/file/2023/4/48374_0s.jpg)
U盘安装原版的xp后进不了系统出现安装后重启怎么办
苹果Mac屏幕录制分享软件:Berrycast
![](https://img.aspxhome.com/file/2023/4/a206954_0s.jpeg)
地平线4加载失败发生不明错误的解决方法
![](https://img.aspxhome.com/file/2023/4/a303335_0s.jpg)
Word2016"此功能看似已中断 并需要修复"怎么办?
![](https://img.aspxhome.com/file/2023/7/17887_0s.png)
新电脑win10分区教程_新电脑win10分区图文教程
![](https://img.aspxhome.com/file/2023/5/a316038_0s.png)
Win10电脑使用AutoCAD闪退怎么办?
![](https://img.aspxhome.com/file/2023/4/51544_0s.jpg)
在Word2010中筛选邮件合并收件人
![](https://img.aspxhome.com/file/2023/2/21752_0s.jpg)