ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 354|回复: 0

工作簿批量合并代码求修改

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-12-29 11:21 | 显示全部楼层 |阅读模式
本帖最后由 pyxzx 于 2021-12-29 11:25 编辑

在网上找到一个合并文件夹下所有工作簿的代码
1.求高手加个注释,太高深看不懂
2.这个代码有一个错误,它会多复制一个表格,最上面和最下面的一样(未验证中间是否错误)该怎么修改会正确?
3.如果文件夹下有多个扩展名的文件,该如何修改?(比如有xls的,有xlsx的,有xlsm的)
求高手帮忙解答一下,谢谢

  1. Dim x As Integer '定义个一公有变量记录复制行数
  2. '复制多个Excel文件中的内容到一个里
  3. Sub combine()
  4.     x = 1
  5.     Dim folder As String
  6.     Dim count As Integer
  7.     folder = ChooseFolder()
  8.     'count = combineFiles(folder, 'xls')
  9.     count = count + combineFiles(folder, "xlsm")
  10.     MsgBox (" succeed ")
  11. End Sub
  12. Function combineFiles(folder, appendix)
  13.     Dim MyFile As String
  14.     Dim s As String
  15.     Dim count, n, copiedlines As Integer
  16.     MyFile = Dir(folder & "\*." & appendix)
  17.     count = count + 1
  18.     n = 2
  19.     Do While MyFile <> ""
  20.         copiedlines = CopyFile(folder & "" & MyFile, 2, n)
  21.         If copiedlines > 0 Then
  22.             n = n + copiedlines
  23.             count = count + 1
  24.         End If
  25.         MyFile = Dir
  26.         Loop
  27.         combineFiles = count
  28.         End Function
  29. '复制数据
  30. Function CopyFile(filename, srcStartLine, dstStartLine)
  31.     Dim book As Workbook
  32.     Dim sheet As Worksheet
  33.     Dim rc As Integer
  34.     CopyFile = 0
  35.     If filename = (ThisWorkbook.Path & "" & ThisWorkbook.Name) Then
  36.         Exit Function
  37.     End If
  38.     Set book = Workbooks.Open(filename)
  39.     ThisWorkbook.Activate
  40.     For j = 1 To book.Sheets.count
  41.         book.Sheets(j).UsedRange.Copy Cells(x, 1)
  42.         With ActiveSheet.Cells.Find("*", Cells(1, 1), -4163, 1, 1, 2).MergeArea '通用判断最后一行
  43.             Rows(.Row + .Rows.count - 1).Select
  44.             x = Selection.Row + Selection.Rows.count - 1
  45.         End With
  46.         x = x + 1
  47.         Next j
  48.         book.Close
  49.         End Function
  50. '选择文件夹
  51. Function ChooseFolder() As String
  52.     Dim dlgOpen As FileDialog
  53.     Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
  54.     With dlgOpen
  55.         If .Show = -1 Then
  56.             ChooseFolder = .SelectedItems(1)
  57.         End If
  58.     End With
  59.     Set dlgOpen = Nothing
  60. End Function
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-19 23:12 , Processed in 0.035387 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表