• Excel中VBA实现文件夹表格合并和数据提取


    多个文件夹下excel文件的提取复制

    1. Public Sub 取出文件()
    2. Dim x1, x2, x3, x4, x5, arr
    3. Dim mysheet1, fs, fo, fd, fi, fe
    4. Dim path, tofile
    5. 'On Error Resume Next '忽略运行过程中可能出现的错误
    6. Application.ScreenUpdating = False '关闭显示更新,提高运行速度
    7. x1 = 2 '从第2行开始
    8. 'Set mysheet1 = ThisWorkbook.Worksheets("Sheet1") '定义Sheet1
    9. path = ThisWorkbook.path + "\正常"
    10. Set fs = CreateObject("Scripting.FileSystemObject") '访问计算机文件
    11. Set fo = fs.GetFolder(path) '该路径下的文件夹
    12. For Each fd In fo.SubFolders '获取该文件夹下面所有的子文件夹
    13. ' Debug.Print fd.path
    14. Debug.Print fd.Name
    15. Set fo = fs.GetFolder(fd.path)
    16. Set fi = fo.Files
    17. For Each fe In fi '获取该文件夹下面所有的子文件
    18. Debug.Print fe.path
    19. tofile = ThisWorkbook.path + "\汇总\" + fd.Name + "+" + fe.Name '复制文件夹
    20. FileCopy fe.path, tofile
    21. Next
    22. Next
    23. Application.ScreenUpdating = True '恢复显示更新
    24. End Sub

    获取多个文件夹下文件和文件夹信息

    1. Public Sub 获取文件夹下文件信息()
    2. Dim x1, x2, x3, x4, x5, arr
    3. Dim mysheet1, fs, fo, fd, fi, fe
    4. On Error Resume Next '忽略运行过程中可能出现的错误
    5. Application.ScreenUpdating = False '关闭显示更新,提高运行速度
    6. x1 = 2 '从第2行开始
    7. Set mysheet1 = ThisWorkbook.Worksheets("Sheet1") '定义Sheet1
    8. Set fs = CreateObject("Scripting.FileSystemObject") '访问计算机文件
    9. mysheet1.Cells(x1, 1) = "C:\Users\Administrator\Desktop\汇总" '查找该文件夹《ABCD》下面所有的文件
    10. For x2 = 2 To 1000000 '预计的文件夹数量
    11. If mysheet1.Cells(x2, 1) <> "" Then
    12. Set fo = fs.GetFolder(mysheet1.Cells(x2, 1)) '该路径下的文件夹
    13. For Each fd In fo.SubFolders '获取该文件夹下面所有的子文件夹
    14. x1 = x1 + 1
    15. arr = Array(fd.path, fd.Name, fd.Type, fd.DateCreated, fd.DateLastModified, fd.Size)
    16. '获取文件路径、名称、类型、创建时间、最后修改时间、大小
    17. For x5 = 0 To 5
    18. mysheet1.Cells(x1, x5 + 1) = arr(x5) '逐一写入单元格
    19. Next
    20. Next
    21. Else
    22. Exit For '退出For循环
    23. End If
    24. Next
    25. x4 = x1
    26. For x3 = 2 To x4
    27. Set fo = fs.GetFolder(mysheet1.Cells(x3, 1))
    28. Set fi = fo.Files
    29. For Each fe In fi '获取该文件夹下面所有的子文件
    30. x1 = x1 + 1
    31. arr = Array(fe.path, fe.Name, fe.Type, fe.DateCreated, fe.DateLastModified, fe.Size)
    32. For x5 = 0 To 5
    33. mysheet1.Cells(x1, x5 + 1) = arr(x5)
    34. Next
    35. Next
    36. Next
    37. Application.ScreenUpdating = True '恢复显示更新
    38. End Sub

    单独excel汇总

    1. Public Sub 汇总()
    2. On Error Resume Next
    3. Dim MP, MN, AW, Wbn, wn
    4. Dim wb As Workbook
    5. Dim i, a, b, d, C, e, last_row, ni
    6. Application.ScreenUpdating = False
    7. MP = "C:\Users\Administrator\Desktop\汇总\6月园区奖金统计表汇总" '工作簿路径
    8. MN = Dir(MP & "\" & "*.xlsx") '工作簿路径
    9. AW = ActiveWorkbook.Name
    10. Num = 0
    11. ni = 0
    12. e = 3 '标题栏数量
    13. craftName = "整理" '定义文件名
    14. last_row_clear = ThisWorkbook.Sheets(craftName).Cells(Rows.Count, "ai").End(xlUp).Row '最后一行位置
    15. If last_row_clear >= 2 Then
    16. ThisWorkbook.Sheets(craftName).Rows("2:" & last_row_clear).Delete
    17. End If
    18. Do While MN <> ""
    19. If MN <> AW Then
    20. ni = ni + 1 '判断导入表的顺序
    21. Debug.Print "导入第" & ni & "表"
    22. Set wb = Workbooks.Open(MP & "\" & MN)
    23. a = a + 1
    24. '工作簿判断
    25. ' Newbook.Sheets.Add.Name = ActiveWorkbook.Name & Wb.ActiveSheet.Name
    26. With ThisWorkbook.Sheets(craftName)
    27. d = wb.Sheets(craftName).UsedRange.Columns.Count '判断列数
    28. C = wb.Sheets(craftName).Cells(Rows.Count, "ai").End(xlUp).Row 'Wb.Sheets(1).UsedRange.Rows.Count - 1 '判断行数
    29. Debug.Print d & "=" & C
    30. Debug.Print wb.Sheets(craftName).Name&; "单表最后一行" & C
    31. last_row = .Cells(Rows.Count, "ai").End(xlUp).Row '最后一行位置
    32. Debug.Print "终表最后一行" & last_row
    33. wb.Sheets(craftName).Range("A1:AL" & C).Copy .Cells(last_row + 1, 1) '复制数据
    34. wn = wb.Sheets(craftName).Name
    35. .Cells(4, "AM") = "表名"
    36. .Cells(e + 1, "AM").Resize(C - 2, 1) = MN & wn
    37. e = e + C '累计行数
    38. .Range("A:L").RowHeight = 12 '行高
    39. .Range("C:C").ColumnWidth = 35 '列宽
    40. Wbn = Wbn & Chr(13) & wb.Name
    41. wb.Close False
    42. End With
    43. End If
    44. MN = Dir
    45. Loop
    46. 'Newbook.SaveAs Filename:=MP & "\" & "进出库汇总3.xlsx"
    47. Range("a1").Select
    48. Application.ScreenUpdating = True
    49. MsgBox "共合并了" & a & "个工作薄下全部工作表。如下:" & Chr(13) & Wbn, vbInformation, "提示"
    50. ThisWorkbook.Sheets(craftName).Range("A:AM").EntireColumn.AutoFit
    51. End Sub
    52. Public Sub 单独汇总()
    53. 'On Error Resume Next
    54. Dim path
    55. Dim wb
    56. Dim name_old, nane_new, wn
    57. Dim C, C1, C2, last_row
    58. Dim file_name
    59. Dim e
    60. file_name = "钱塘&东港\东港&钱塘月度奖励发放统计表(6月).xlsx" '汇总数据源表格路径
    61. path = ThisWorkbook.path + "\6月园区奖金统计表汇总\" + file_name
    62. Debug.Print ThisWorkbook.path
    63. Set wb = Workbooks.Open(path)
    64. 'name_old = "园所转化奖金核算(6月)"
    65. nane_new = "园所转化奖金核算(6月)" '目标工作表名称
    66. '第一张表
    67. With ThisWorkbook.Sheets(nane_new)
    68. C = wb.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets( last_row).UsedRange.Rows.Count - 1 '判断行数
    69. last_row = .Cells(Rows.Count, "A").End(xlUp).Row '最后一行位置
    70. wb.Sheets(1).Range("A1:V" & C).Copy .Cells(last_row + 1, 1) '复制数据
    71. wn = wb.Sheets(1).Name
    72. .Cells(4, "W") = "表名"
    73. .Cells(last_row + 1, "W").Resize(C - 2, 1) = file_name & "/" & wn
    74. ' e = e + C '累计行数
    75. End With
    76. '第二张表
    77. With ThisWorkbook.Sheets("普惠园月度托班招生人数奖励(6月)")
    78. C1 = wb.Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets( last_row).UsedRange.Rows.Count - 1 '判断行数
    79. last_row = .Cells(Rows.Count, "A").End(xlUp).Row '最后一行位置
    80. wb.Sheets(2).Range("A1:H" & C1).Copy .Cells(last_row + 1, 1) '复制数据
    81. wn = wb.Sheets(2).Name
    82. .Cells(4, "I") = "表名"
    83. .Cells(last_row + 1, "I").Resize(C1 - 2, 1) = file_name & "/" & wn
    84. ' e = e + last_row '累计行数
    85. Debug.Print e
    86. End With
    87. '第三张表
    88. With ThisWorkbook.Sheets("课程顾问月度提成详表(6月)")
    89. C2 = wb.Sheets(3).Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets( last_row).UsedRange.Rows.Count - 1 '判断行数
    90. last_row = .Cells(Rows.Count, "A").End(xlUp).Row '最后一行位置
    91. wb.Sheets(3).Range("A1:Z" & C2).Copy .Cells(last_row + 1, 1) '复制数据
    92. wn = wb.Sheets(3).Name
    93. .Cells(4, "AD") = "表名"
    94. .Cells(last_row + 1, "AD").Resize(C2 - 2, 1) = file_name & "/" & wn
    95. ' e = e + last_row '累计行数
    96. ' Debug.Print e
    97. End With
    98. wb.Close False
    99. End Sub

    excel数据批量汇总‘

    1. Sub 批量汇总()
    2. Dim wb
    3. Dim mypath, myfile, a, nane_new
    4. Dim path
    5. Dim name_old, wn
    6. Dim C, C1, C2, last_row
    7. Dim file_name
    8. Dim e
    9. mypath = ThisWorkbook.path + "\汇总\"
    10. myfile = Dir(mypath, vbDirectory)
    11. a = 1
    12. Do While myfile <> ""
    13. If myfile <> "." And myfile <> ".." Then
    14. path = mypath + myfile
    15. 'Debug.Print path
    16. Set wb = Workbooks.Open(path)
    17. 'name_old = "园所转化奖金核算(6月)"
    18. nane_new = "园所转化奖金核算(6月)" '目标工作表名称
    19. '第一张表
    20. With ThisWorkbook.Sheets(nane_new)
    21. C = wb.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets( last_row).UsedRange.Rows.Count - 1 '判断行数
    22. last_row = .Cells(Rows.Count, "A").End(xlUp).Row '最后一行位置
    23. wb.Sheets(1).Range("A1:V" & C).Copy .Cells(last_row + 1, 1) '复制数据
    24. wn = wb.Sheets(1).Name
    25. .Cells(4, "W") = "表名"
    26. .Cells(last_row + 2, "W").Resize(C - 1, 1) = myfile & "/" & wn
    27. ' e = e + C '累计行数
    28. End With
    29. '第二张表
    30. With ThisWorkbook.Sheets("普惠园月度托班招生人数奖励(6月)")
    31. C1 = wb.Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets( last_row).UsedRange.Rows.Count - 1 '判断行数
    32. last_row = .Cells(Rows.Count, "A").End(xlUp).Row '最后一行位置
    33. wb.Sheets(2).Range("A1:H" & C1).Copy .Cells(last_row + 1, 1) '复制数据
    34. wn = wb.Sheets(2).Name
    35. .Cells(4, "I") = "表名"
    36. .Cells(last_row + 2, "I").Resize(C1 - 1, 1) = myfile & "/" & wn
    37. ' e = e + last_row '累计行数
    38. Debug.Print e
    39. End With
    40. '第三张表
    41. With ThisWorkbook.Sheets("课程顾问月度提成详表(6月)")
    42. C2 = wb.Sheets(3).Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets( last_row).UsedRange.Rows.Count - 1 '判断行数
    43. last_row = .Cells(Rows.Count, "A").End(xlUp).Row '最后一行位置
    44. wb.Sheets(3).Range("A1:Z" & C2).Copy .Cells(last_row + 1, 1) '复制数据
    45. wn = wb.Sheets(3).Name
    46. .Cells(4, "AD") = "表名"
    47. .Cells(last_row + 2, "AD").Resize(C2 - 1, 1) = myfile & "/" & wn
    48. ' e = e + last_row '累计行数
    49. ' Debug.Print e
    50. End With
    51. wb.Close False
    52. a = a + 1
    53. myfile = Dir
    54. Else
    55. myfile = Dir
    56. End If
    57. Loop
    58. End Sub

  • 相关阅读:
    UI自动化 --- UI Automation 基础详解
    lodash库_.chunk、_.pick、_.omit、_.cloneDeep、_.debounce方法
    Day815.数据库参数设置优化 -Java 性能调优实战
    二十一、SpringBoot + Jwt + Vue 权限管理系统 (2)
    Spring底层架构核心概念
    Ubuntu下使用Docker的简单命令
    pytest+request+allure+excel接口自动化搭建 从0到1【二 读取Mysql用例】
    Jenkins自动构建(Gitee)
    产品与技术的平衡
    基于Qt 的CAN Bus实现
  • 原文地址:https://blog.csdn.net/u010719791/article/details/126111306