使用VBA批量获取并合并指定目录下的Excel文件内容
创作时间:
作者:
@小白创作中心
使用VBA批量获取并合并指定目录下的Excel文件内容
引用
CSDN
1.
https://blog.csdn.net/weixin_43957681/article/details/138134760
在日常工作中,我们常常需要处理大量分散在不同Excel文件中的数据。手动合并这些数据不仅耗时,还容易出错。本文将介绍如何使用VBA编写一个脚本,自动获取指定目录下的所有Excel文件,并将它们的内容合并到一个新的Excel文件中。
1.新建一个excel表格。并创建两个Sheet,名字分别命名为FileList 和 All information。
2.按ALT+F11进入 VBA编程模块,插入模块。
3.将如下 第五部分代码复制到模块中。 点击运行即可,然后就能提取指定目录下的所有excel文件信息并合并到一起输出到“All information” 中。
4.运行过程中,在弹窗中输入 想要提取信息的路径地址。
5.说明
这个脚本的逻辑分为两部分:
- 首先是提取文件夹中所有文件的基本信息,并将其填充到"FileList"工作表中。
- 之后,它将这些文件打开并将它们的内容合并到"All information"工作表中。
Sub CombinedScript()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error Resume Next
' Step 1: Extracting files from folders
Dim arr(1 To 10000) As String
Dim arr1(1 To 100000, 1 To 6) As String
Dim fso As Object, myfile As Object
Dim f, i, k, f2, f3, x
Dim q As Integer
arr(1) = Application.InputBox("Please enter the path to scan") & "\"
i = 1
k = 1
Do While i < UBound(arr)
If arr(i) = "" Then Exit Do
f = Dir(arr(i), vbDirectory)
Do
If InStr(f, ".") = 0 And f <> "" Then
k = k + 1
arr(k) = arr(i) & f & "\"
End If
f = Dir
Loop Until f = ""
i = i + 1
Loop
' Extract files information
Set fso = CreateObject("Scripting.FileSystemObject")
For x = 1 To UBound(arr)
If arr(x) = "" Then Exit For
f3 = Dir(arr(x) & "*.*")
Do While f3 <> ""
If InStr(f3, ".") > 0 Then
q = q + 1
arr1(q, 5) = arr(x) & f3
Set myfile = fso.GetFile(arr1(q, 5))
arr1(q, 1) = f3
arr1(q, 2) = myfile.Size
arr1(q, 3) = myfile.DateCreated
arr1(q, 4) = myfile.DateLastModified
arr1(q, 6) = myfile.DateLastAccessed
End If
f3 = Dir
Loop
Next x
Sheets("FileList").Range("A2").Resize(1000, 6).ClearContents
Sheets("FileList").Range("A2").Resize(q, 6) = arr1
' Step 2: Combine information into "All information" sheet
If Sheets("All information").FilterMode = True Then
Sheets("All information").ShowAllData
End If
Sheets("All information").Range("A2:ZZ100000").ClearContents
Dim currentFile As Object
Dim targetRow As Integer
Dim temRowCount As Integer
targetRow = 2
For fileCount = 2 To Sheets("FileList").Cells(10000, 1).End(xlUp).Row
Set currentFile = Application.Workbooks.Open(Sheets("FileList").Cells(fileCount, 5))
For sheetscount = 1 To currentFile.Sheets.Count
temRowCount = currentFile.Sheets(sheetscount).UsedRange.Rows.Count
' Copy content
currentFile.Sheets(sheetscount).UsedRange.Copy
ThisWorkbook.Sheets("All information").Cells(targetRow, 3).PasteSpecial (xlPasteValues)
' Set sheet and workbook information
ThisWorkbook.Sheets("All information").Range("A" & targetRow & ":A" & targetRow + temRowCount).Value = currentFile.Name
ThisWorkbook.Sheets("All information").Range("B" & targetRow & ":B" & targetRow + temRowCount).Value = currentFile.Sheets(sheetscount).Name
targetRow = targetRow + temRowCount
Next sheetscount
currentFile.Close False
Next fileCount
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
热门推荐
高阶技术:采用瑞士奶酪模型的结构化方法,以减少填充剂注射的血管不良事件
拥有4090显卡必读:如何选择高性能CPU提升游戏体验
小红书账号被封怎么办?5步教你轻松恢复权限
干细胞研究与应用——为人类生命健康提供保障
成长之路:雷阿伦的职业生涯全纪录
人工智能AI通用分级标准方法
空调取暖费电吗?如何高效节能取暖
脚上起红疹子很痒是怎么回事
喜欢户外露营?一旦被叮,奇痒无比!
香松怎么养殖?家里为什么不能养香松?
专家解读:中成药能否一起服用?需要注意哪些问题?
消费贷款都有哪些申请技巧
哪吒汽车生死时刻:被传研发团队解散、供应商围堵总部 百亿债务压顶,自救之路何在
AI风险审计方法论
揭秘DeepMind如何对抗操纵性AI:新研究为减轻说服性AI的危害铺路
泉州发起姜母鸭产业调研 酝酿制定地方制作技艺标准
不须杂术自长生,精、气、神炼三而一的两种方法!
四川乐山大佛及周边景点深度自驾游攻略:行程规划、必游之地与实用 tips
如何引导孩子面对失败
双影奇境卡顿怎么解决?5种实用解决方案
上市公司退市“八问八答”
产品退市阶段,如何管理好客户满意度并共同探索新的商业机会?
养鱼新手必看!过滤设备选择与使用技巧
这些植物种在家里能驱虫赏花,种在地里能防虫,你也赶紧试试吧
[2025] 摩周湖完全攻略|周边推荐景点及交通方式
蚂蚁后是什么?揭秘蚁后:掌控蚁群的幕后女王!
花粉、柳絮过敏患者用什么药
紫藤移植地栽埋土深度多少
面对裁员如何争取利益?这份法律指南请收好
为什么你时常感到“羞耻”?