【VBA】获取指定目录下的Excel文件,并合并所有excel中的内容。
创作时间:
作者:
@小白创作中心
【VBA】获取指定目录下的Excel文件,并合并所有excel中的内容。
引用
CSDN
1.
https://blog.csdn.net/weixin_43957681/article/details/138134760
本文将介绍如何使用VBA代码实现获取指定目录下的Excel文件,并将这些Excel文件中的内容合并到一个新的Excel文件中。
操作步骤
新建一个Excel表格,并创建两个Sheet,名字分别命名为FileList 和 All information。
按ALT+F11进入 VBA编程模块,插入模块。
将以下代码复制到模块中。点击运行即可,然后就能提取指定目录下的所有Excel文件信息并合并到一起输出到“All information” 中。
运行过程中,在弹窗中输入想要提取信息的路径地址。
代码说明
这个脚本的逻辑分为两部分:
- 首先是提取文件夹中所有文件的基本信息,并将其填充到"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
本文原文来自CSDN
热门推荐
酸樱桃助眠效果超26%,香蕉核桃番茄也能改善睡眠
专家解读褪黑素:不是人人都适用的“助眠神药”
王羲之教你玩转草书艺术
草字头汉字:从自然观察到文化传承
六招提升老人安全意识,全方位守护晚年生活
解热镇痛药伤胃风险高,胃肠病患者用药指南
儿童发热用药指南:何时用、怎么用、呕吐后怎么办
解热镇痛药与氯丙嗪降温机制大不同,适用场景有讲究
养老院成“虐老重灾区”,四种新型养老模式能否破解困局
婺源春色:万亩梯田油菜花与千年徽派建筑的完美融合
2024甲辰龙年:龙文化的前世今生
“龙年”翻译热议:一场文化对话的新开端
龙年翻译热议:Loong vs Dragon,你怎么看?
如何进行项目拆解管理实施
揭秘古代炼丹炉:道教外丹术的神秘面纱
人体丹炉与道家炼丹术的奇妙关系
《西游记》里的炼丹炉:从文学到科技的千年传奇
家庭版红糖红烧肉:简单易学的美味秘诀
红糖红烧肉新玩法:这样做更健康美味!
做做运动,远离“冻结肩”
得了肩周炎怎么办?6个练习帮你缓解疼痛
肩周炎功能锻炼怎么做
一封家书,温暖你我:如何通过过年稿件促进家庭和谐
年味儿:从准备到团聚,从相聚到离别
北京台春晚教你如何写出有底蕴的过年稿件
2025元旦头像怎么选?朋友圈炫酷指南
DIY春节头像,秀出你的新年style!
依柯胰岛素获准上市,2型糖尿病患者每周只需注射一次
黄大年:地球物理界的超级英雄
黄大年:深地探测的科技先锋