EXCEL VBA 跨表合并多个文件
时间:2022-05-19 02:58:30
‘选择一个目录,将目录中的所有EXCEL文件导入当前工作表
‘这些EXCEL文件最好格式能一样,这里是每个文件是同一个格式
Sub 批量()
Dim FD, str$, arr
Set FD = Application.FileDialog(msoFileDialogFolderPicker)
If FD.Show = -1 Then t = FD.SelectedItems(1) Else Exit Sub ‘如果没选择文件夹则退出
Application.ScreenUpdating = False
Cells.NumberFormatLocal = "@"
str = Dir(t & "\*.xl*") ‘查找格式为EXCEL的文件
While Len(str) > 0 ‘文件名不为空时
Workbooks.Open (t & IIf(Right(t, 1) = "", "", "") & str) ‘打开工作簿
With ActiveWorkbook.ActiveSheet
.Range(.Cells(2, "l"), .Cells(.[a65536].End(3).Row, "l")) = "’" & Left(str, Len(str) – IIf(Right(str, 1) = "x", 5, 4))
arr = .UsedRange
Workbooks(str).Close False ‘关闭工作薄
Kill (t & IIf(Right(t, 1) = "", "", "") & str) ’删除工作薄(如果不删除,省去这一步)
End With
With ActiveSheet
rw = .[a65536].End(3).Row + 1
.Cells(rw, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr ‘将数据写入当前工作表
End With
str = Dir() ‘查找下一个文件
Wend
If [a1] = "" Then Rows(1).Delete ‘如果A1为空,删除第一行
Application.ScreenUpdating = True
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
2003版excel表格怎么自动换行
excel2003动态图表制作教程
如何计算excel求和的方法
excel2003筛选功能的使用步骤
如何将pdf转换成word文档-pdf转换成word教程
如何去除word文档中的自动编号
![](https://img.aspxhome.com/file/2023/5/35575_0s.png)
Win10电脑录屏怎么录桌面?Win10录制模式有什么不同?
![](https://img.aspxhome.com/file/2023/7/51657_0s.jpg)
Excel怎么给所有数字批量增加指定值 Excel数字批量增加指定值方法
![](https://img.aspxhome.com/file/2023/4/39734_0s.png)
Excle怎样设置定时器?
word2016中怎么使用记忆式键入功能?
![](https://img.aspxhome.com/file/2023/6/25636_0s.jpg)
excel表格中如何插入超链接
![](https://img.aspxhome.com/file/2023/1/a155811_0s.gif)
word使用过程的一些小技巧
Word技巧比Excel函数更快捷
Excel 数据有效性如何限定输入数据?
![](https://img.aspxhome.com/file/2023/4/38254_0s.jpg)
word页码从任意页开始
![](https://img.aspxhome.com/file/2023/7/22947_0s.jpg)
如何通过Word提供的插入功能将日期和时间快速插入文档
![](https://img.aspxhome.com/file/2023/1/33091_0s.jpg)
Excel表格怎么快速圈出高于80分的成绩?
![](https://img.aspxhome.com/file/2023/0/40250_0s.png)
Win10玩红警黑屏有声音和鼠标怎么解决?
![](https://img.aspxhome.com/file/2023/4/49574_0s.jpg)
excel利用透视表如何成倍提高数据汇总效率?
![](https://img.aspxhome.com/file/2023/8/41428_0s.png)
word2013怎么设置打印方向为横向
![](https://img.aspxhome.com/file/2023/7/19107_0s.jpg)