• 【VBA】获取指定目录下的Excel文件,并合并所有excel中的内容。


    1.新建一个excel表格。并创建两个Sheet,名字分别命名为FileList 和 All information。

    2.按ALT+F11进入  VBA编程模块,插入模块。 

    3.将如下 第五部分代码复制到模块中。  点击运行即可,然后就能提取指定目录下的所有excel文件信息并合并到一起输出到“All information” 中。

    4.运行过程中,在弹窗中输入 想要提取信息的路径地址。

    5.说明

    这个脚本的逻辑分为两部分:

    • 首先是提取文件夹中所有文件的基本信息,并将其填充到"FileList"工作表中。
    • 之后,它将这些文件打开并将它们的内容合并到"All information"工作表中。
    1. Sub CombinedScript()
    2. Application.DisplayAlerts = False
    3. Application.ScreenUpdating = False
    4. On Error Resume Next
    5. ' Step 1: Extracting files from folders
    6. Dim arr(1 To 10000) As String
    7. Dim arr1(1 To 100000, 1 To 6) As String
    8. Dim fso As Object, myfile As Object
    9. Dim f, i, k, f2, f3, x
    10. Dim q As Integer
    11. arr(1) = Application.InputBox("Please enter the path to scan") & "\"
    12. i = 1
    13. k = 1
    14. Do While i < UBound(arr)
    15. If arr(i) = "" Then Exit Do
    16. f = Dir(arr(i), vbDirectory)
    17. Do
    18. If InStr(f, ".") = 0 And f <> "" Then
    19. k = k + 1
    20. arr(k) = arr(i) & f & "\"
    21. End If
    22. f = Dir
    23. Loop Until f = ""
    24. i = i + 1
    25. Loop
    26. ' Extract files information
    27. Set fso = CreateObject("Scripting.FileSystemObject")
    28. For x = 1 To UBound(arr)
    29. If arr(x) = "" Then Exit For
    30. f3 = Dir(arr(x) & "*.*")
    31. Do While f3 <> ""
    32. If InStr(f3, ".") > 0 Then
    33. q = q + 1
    34. arr1(q, 5) = arr(x) & f3
    35. Set myfile = fso.GetFile(arr1(q, 5))
    36. arr1(q, 1) = f3
    37. arr1(q, 2) = myfile.Size
    38. arr1(q, 3) = myfile.DateCreated
    39. arr1(q, 4) = myfile.DateLastModified
    40. arr1(q, 6) = myfile.DateLastAccessed
    41. End If
    42. f3 = Dir
    43. Loop
    44. Next x
    45. Sheets("FileList").Range("A2").Resize(1000, 6).ClearContents
    46. Sheets("FileList").Range("A2").Resize(q, 6) = arr1
    47. ' Step 2: Combine information into "All information" sheet
    48. If Sheets("All information").FilterMode = True Then
    49. Sheets("All information").ShowAllData
    50. End If
    51. Sheets("All information").Range("A2:ZZ100000").ClearContents
    52. Dim currentFile As Object
    53. Dim targetRow As Integer
    54. Dim temRowCount As Integer
    55. targetRow = 2
    56. For fileCount = 2 To Sheets("FileList").Cells(10000, 1).End(xlUp).Row
    57. Set currentFile = Application.Workbooks.Open(Sheets("FileList").Cells(fileCount, 5))
    58. For sheetscount = 1 To currentFile.Sheets.Count
    59. temRowCount = currentFile.Sheets(sheetscount).UsedRange.Rows.Count
    60. ' Copy content
    61. currentFile.Sheets(sheetscount).UsedRange.Copy
    62. ThisWorkbook.Sheets("All information").Cells(targetRow, 3).PasteSpecial (xlPasteValues)
    63. ' Set sheet and workbook information
    64. ThisWorkbook.Sheets("All information").Range("A" & targetRow & ":A" & targetRow + temRowCount).Value = currentFile.Name
    65. ThisWorkbook.Sheets("All information").Range("B" & targetRow & ":B" & targetRow + temRowCount).Value = currentFile.Sheets(sheetscount).Name
    66. targetRow = targetRow + temRowCount
    67. Next sheetscount
    68. currentFile.Close False
    69. Next fileCount
    70. Application.DisplayAlerts = True
    71. Application.ScreenUpdating = True
    72. End Sub

  • 相关阅读:
    某验四代滑块验证码逆向分析
    计算机Android毕业设计论文基于Uniapp+SSM实现的作业管理app
    cad转换成pdf怎么转?
    字符串编辑距离
    Java面试题以及答案---FastDFS
    DDD技术方案落地实践 | 京东云技术团队
    npx 初始化 React 项目 踩坑记录
    AI低代码维格云甘特视图怎么用?
    新唐NUC980使用记录:向内核添加USB无线网卡驱动(基于RTL8188EUS)
    飞翔的小鸟
  • 原文地址:https://blog.csdn.net/weixin_43957681/article/details/138134760