ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[已解决] 遍历文件夹下的工作簿、工作表进行取数,无需打开文件,求助。

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2012-4-15 22:56 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 心电感应 于 2012-4-15 23:06 编辑

    现有一批数据特点如下:

1、工作簿在该文件夹会有增加,无规律,需要遍历文件夹。
2、每个工作簿的工作表都会有增加,名称无规律,工作表也要遍历。
3、每个工作表的结构完全相同,项目字段内容有空值或零值的可能,如果遇此情况就返回相应的空值或零值。
4、希望返回的数据有工作簿名称、工作表名称等等字段,详细请看附件。

    鄙人对VBA也还不太清楚,会看会改一些代码以符合自己的需求,希望EH坛友帮忙,谢谢!
HelpSample.zip (34.9 KB, 下载次数: 246)

TA的精华主题

TA的得分主题

发表于 2012-4-15 23:46 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 zhaogang1960 于 2012-4-16 00:30 编辑

短信收到,第一个问题请参考:
  1. Sub Macro1()
  2. '引用Microsoft AD0 Ext 2.8 for DDL and Security
  3. '引用Microsoft ActiveX Data Objects 2.x Library
  4.     Dim cnn As New ADODB.Connection
  5.     Dim rs As ADODB.Recordset
  6.     Dim cat  As New ADOX.Catalog, tb1 As Table
  7.     Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, t$, n%
  8.     Mypath = ThisWorkbook.Path & ""
  9.     MyFile = Dir(Mypath & "*.xlsx")
  10.     Do While MyFile <> ""
  11.         If MyFile <> ThisWorkbook.Name Then
  12.             n = n + 1
  13.             If n > 1 Then
  14.                 t = "[Excel 12.0;Database=" & Mypath & MyFile & "]."
  15.             Else
  16.                 cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile
  17.             End If
  18.             cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
  19.             For Each tb1 In cat.Tables
  20.                 If tb1.Type = "TABLE" Then
  21.                     s = Replace(tb1.Name, "'", "")
  22.                     If Right(s, 1) = "$" Then
  23.                         m = m + 1
  24.                         If m = 1 Then
  25.                             Set rs = cnn.Execute("[" & s & "a5:d]")
  26.                             For i = 0 To rs.Fields.Count - 1
  27.                                 temp = temp & rs.Fields(i).Name & ","
  28.                             Next
  29.                         End If
  30.                         If SQL <> "" Then SQL = SQL & " union all "
  31.                         SQL = SQL & "select " & temp & "'" & Replace(s, "$", "") & "' as 工作表,'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿 from " & t & "[" & s & "a5:d]"
  32.                     End If
  33.                 End If
  34.             Next
  35.         End If
  36.         MyFile = Dir()
  37.     Loop
  38.     [a1].CurrentRegion.Offset(3).ClearContents
  39.     [a4].CopyFromRecordset cnn.Execute(SQL)
  40.     cnn.Close
  41.     Set rs = Nothing
  42.     Set cnn = Nothing
  43.     Set cat = Nothing
  44.     Set tb1 = Nothing
  45. End Sub
复制代码

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2012-4-15 23:47 | 显示全部楼层
本帖最后由 zhaogang1960 于 2012-4-16 00:31 编辑

请看附件
HelpSample.rar (50.16 KB, 下载次数: 412)

TA的精华主题

TA的得分主题

发表于 2012-4-16 01:03 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
第二个问题:
  1. Sub Macro2()
  2. '引用Microsoft AD0 Ext 2.8 for DDL and Security
  3. '引用Microsoft ActiveX Data Objects 2.x Library
  4.     Dim cnn As New ADODB.Connection
  5.     Dim rs As ADODB.Recordset
  6.     Dim cat  As New ADOX.Catalog, tb1 As Table
  7.     Dim SQL$, MyFile$, i%, temp$, strField$, s$, t$, n%
  8.     Mypath = ThisWorkbook.Path & ""
  9.     MyFile = Dir(Mypath & "*.xlsx")
  10.     Do While MyFile <> ""
  11.         If MyFile <> ThisWorkbook.Name Then
  12.             n = n + 1
  13.             If n > 1 Then
  14.                 t = "[Excel 12.0;HDR=No;Database=" & Mypath & MyFile & "]."
  15.             Else
  16.                 cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
  17.             End If
  18.             cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
  19.             For Each tb1 In cat.Tables
  20.                 If tb1.Type = "TABLE" Then
  21.                     s = Replace(tb1.Name, "'", "")
  22.                     If Right(s, 1) = "$" Then
  23.                         If SQL <> "" Then SQL = SQL & " union all "
  24.                         SQL = SQL & "select " & "'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿,'" & Replace(s, "$", "") & "' as 工作表,(select f1 from " & t & "[" & s & "b1:b1]),(select f1 from " & t & "[" & s & "b3:b3]),(select f1 from " & t & "[" & s & "e1:e1]) from " & t & "[" & s & "a1:e1]"
  25.                     End If
  26.                 End If
  27.             Next
  28.         End If
  29.         MyFile = Dir()
  30.     Loop
  31.     [a1].CurrentRegion.Offset(3).ClearContents
  32.     [a4].CopyFromRecordset cnn.Execute(SQL)
  33.     cnn.Close
  34.     Set rs = Nothing
  35.     Set cnn = Nothing
  36.     Set cat = Nothing
  37.     Set tb1 = Nothing
  38. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2012-4-16 07:24 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
赵老师V5,留个脚印先。。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-4-16 18:25 | 显示全部楼层
本帖最后由 whggwu 于 2012-4-16 18:42 编辑
zhaogang1960 发表于 2012-4-16 01:03
第二个问题:


深夜的回复,感激不尽!

不过使用的时候,第一个问题,文件夹下工作簿超过18(含18)个(不含总表)就提示运行错误,查询过于复杂,请问是本身就有一定局限性不能执行太多,还是什么原因嗯?
有没有其他办法,不用VBA也行,或者加载其他工具也行。
实在不行我就一次17个文件取数一次好了,赵老师不用那么费心了,还是非常感谢!

TA的精华主题

TA的得分主题

发表于 2012-4-16 18:45 | 显示全部楼层
两个问题合并:
  1. Sub Macro1()
  2. '引用Microsoft AD0 Ext 2.8 for DDL and Security
  3. '引用Microsoft ActiveX Data Objects 2.x Library
  4.     Dim cnn As New ADODB.Connection
  5.     Dim rs As ADODB.Recordset
  6.     Dim cat  As New ADOX.Catalog, tb1 As Table
  7.     Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, t$, t2$, n%
  8.     Mypath = ThisWorkbook.Path & ""
  9.     MyFile = Dir(Mypath & "*.xlsx")
  10.     Do While MyFile <> ""
  11.         If MyFile <> ThisWorkbook.Name Then
  12.             n = n + 1
  13.             If n > 1 Then
  14.                 t = "[Excel 12.0;Database=" & Mypath & MyFile & "]."
  15.             Else
  16.                 cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile
  17.             End If
  18.             t2 = "[Excel 12.0;HDR=No;Database=" & Mypath & MyFile & "]."
  19.             cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
  20.             For Each tb1 In cat.Tables
  21.                 If tb1.Type = "TABLE" Then
  22.                     s = Replace(tb1.Name, "'", "")
  23.                     If Right(s, 1) = "$" Then
  24.                         m = m + 1
  25.                         If m = 1 Then
  26.                             Set rs = cnn.Execute("[" & s & "a5:d]")
  27.                             For i = 0 To rs.Fields.Count - 1
  28.                                 temp = temp & rs.Fields(i).Name & ","
  29.                             Next
  30.                         End If
  31.                         
  32.                         If SQL <> "" Then SQL = SQL & " union all "
  33.                         SQL = SQL & "select " & temp & "'" & Replace(s, "$", "") & "' as 工作表,'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿 from " & t & "[" & s & "a5:d]"
  34.                         
  35.                         If SQL2 <> "" Then SQL2 = SQL2 & " union all "
  36.                         SQL2 = SQL2 & "select " & "'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿,'" & Replace(s, "$", "") & "' as 工作表,(select f1 from " & t2 & "[" & s & "b1:b1]),(select f1 from " & t2 & "[" & s & "b3:b3]),(select f1 from " & t2 & "[" & s & "e1:e1]) from " & t2 & "[" & s & "a1:e1]"
  37.                     End If
  38.                 End If
  39.             Next
  40.         End If
  41.         MyFile = Dir()
  42.     Loop
  43.     With Sheets("项目取数")
  44.         .[a1].CurrentRegion.Offset(3).ClearContents
  45.         .[a4].CopyFromRecordset cnn.Execute(SQL)
  46.     End With
  47.     With Sheets("基础信息")
  48.         .[a1].CurrentRegion.Offset(3).ClearContents
  49.         .[a4].CopyFromRecordset cnn.Execute(SQL2)
  50.     End With
  51.     cnn.Close
  52.     Set rs = Nothing
  53.     Set cnn = Nothing
  54.     Set cat = Nothing
  55.     Set tb1 = Nothing
  56. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-4-16 18:53 | 显示全部楼层
zhaogang1960 发表于 2012-4-16 18:45
两个问题合并:

yunxincuowu.jpg yunxincuowu2.jpg
超过18个就会提示

TA的精华主题

TA的得分主题

发表于 2012-4-16 19:16 | 显示全部楼层
whggwu 发表于 2012-4-16 18:53
超过18个就会提示

什么超过18个?

TA的精华主题

TA的得分主题

发表于 2012-4-16 19:23 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
是文件数超过18个?确切地说是工作表数超过49个,如果是需要分步复制数据,请测试:
  1. Sub Macro1()
  2. '引用Microsoft AD0 Ext 2.8 for DDL and Security
  3. '引用Microsoft ActiveX Data Objects 2.x Library
  4.     Dim cnn As New ADODB.Connection
  5.     Dim rs As ADODB.Recordset
  6.     Dim cat  As New ADOX.Catalog, tb1 As Table
  7.     Dim d As Object, ds As Object
  8.     Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, t$, t2$, n%
  9.     Application.ScreenUpdating = False
  10.     Set d = CreateObject("scripting.dictionary")
  11.     Set ds = CreateObject("scripting.dictionary")
  12.     Sheets("项目取数").[a1].CurrentRegion.Offset(3).ClearContents
  13.     Sheets("基础信息").[a1].CurrentRegion.Offset(3).ClearContents
  14.     Mypath = ThisWorkbook.Path & ""
  15.     MyFile = Dir(Mypath & "*.xlsx")
  16.     Do While MyFile <> ""
  17.         If MyFile <> ThisWorkbook.Name Then
  18.             n = n + 1
  19.             If n > 1 Then
  20.                 t = "[Excel 12.0;Database=" & Mypath & MyFile & "]."
  21.             Else
  22.                 cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile
  23.             End If
  24.             t2 = "[Excel 12.0;HDR=No;Database=" & Mypath & MyFile & "]."
  25.             cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
  26.             For Each tb1 In cat.Tables
  27.                 If tb1.Type = "TABLE" Then
  28.                     s = Replace(tb1.Name, "'", "")
  29.                     If Right(s, 1) = "$" Then
  30.                         m = m + 1
  31.                         If m = 1 Then
  32.                             Set rs = cnn.Execute("[" & s & "a5:d]")
  33.                             For i = 0 To rs.Fields.Count - 1
  34.                                 temp = temp & rs.Fields(i).Name & ","
  35.                             Next
  36.                         End If
  37.                         SQL = "select " & temp & "'" & Replace(s, "$", "") & "' as 工作表,'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿 from " & t & "[" & s & "a5:d]"
  38.                         d(SQL) = ""
  39.                         SQL2 = "select " & "'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿,'" & Replace(s, "$", "") & "' as 工作表,(select f1 from " & t2 & "[" & s & "b1:b1]),(select f1 from " & t2 & "[" & s & "b3:b3]),(select f1 from " & t2 & "[" & s & "e1:e1]) from " & t2 & "[" & s & "a1:e1]"
  40.                         ds(SQL2) = ""
  41.                         If m Mod 49 = 0 Then Call Replicated_data(d, ds, cnn)
  42.                     End If
  43.                 End If
  44.             Next
  45.         End If
  46.         MyFile = Dir()
  47.     Loop
  48.     If d.Count > 0 Then Call Replicated_data(d, ds, cnn)
  49.     rs.Close
  50.     cnn.Close
  51.     Set rs = Nothing
  52.     Set cnn = Nothing
  53.     Set cat = Nothing
  54.     Set tb1 = Nothing
  55.     Application.ScreenUpdating = True
  56. End Sub

  57. Sub Replicated_data(ByRef d As Object, ByRef ds As Object, ByRef cnn As Object)
  58.     Dim SQL$
  59.     SQL = Join(d.Keys, " UNION ALL ")
  60.     Sheets("项目取数").[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  61.     SQL = Join(ds.Keys, " UNION ALL ")
  62.     Sheets("基础信息").[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  63.     d.RemoveAll
  64.     ds.RemoveAll
  65. End Sub
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-9-30 06:24 , Processed in 0.041341 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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