ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

需要从多表时取得需求的数据填至新表格

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-4-15 17:26 | 显示全部楼层 |阅读模式
各位大神,取数时行,列的内容会跟据附表调整

模板.rar

211.14 KB, 下载次数: 17

TA的精华主题

TA的得分主题

发表于 2024-4-15 18:13 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
附件都不大,为什么打开却很卡?

TA的精华主题

TA的得分主题

发表于 2024-4-15 18:15 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
这个分页预览是不是打开特别卡?

TA的精华主题

TA的得分主题

发表于 2024-4-15 20:00 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
打开工作簿很卡

TA的精华主题

TA的得分主题

发表于 2024-4-15 20:04 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub test1() '练习,仅供测试……
  2.   
  3.   ActiveSheet.UsedRange.Offset(1).Clear
  4.   Application.ScreenUpdating = False
  5.   
  6.   Dim p As String, f As String
  7.   Dim ar, br, dic As Object, dict As Object, target As Range
  8.   Dim Conn As Object, rs As Object, Cata As Object, tb As Object
  9.   Dim strConn As String, SQL As String, s As String, t As String, n As String, i As Long
  10.   
  11.   Set target = Range("A2")
  12.   Set dic = CreateObject("Scripting.Dictionary")
  13.   Set dict = CreateObject("Scripting.Dictionary")
  14.   Set Cata = CreateObject("ADOX.Catalog")
  15.   Set Conn = CreateObject("ADODB.Connection")
  16.   
  17.   ar = Application.Rept(Range("A1").CurrentRegion.Rows(1).Value, 1)
  18.   For i = 1 To UBound(ar)
  19.     s = "`" & Trim(ar(i)) & "`"
  20.     dic.Add s, i
  21.     ar(i) = "NULL AS " & s
  22.   Next
  23.   
  24.   s = "Excel 12.0;HDR=YES;IMEX=1;Database="
  25.   If Application.Version < 12 Then
  26.     s = Replace(s, "12.0", "8.0")
  27.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
  28.   Else
  29.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
  30.   End If
  31.   Conn.Open strConn & ThisWorkbook.FullName
  32.   
  33.   p = ThisWorkbook.Path & "\"
  34.   f = Dir(p & "*.xls*")
  35.   While Len(f)
  36.     If p & f <> ThisWorkbook.FullName Then
  37.       Cata.ActiveConnection = strConn & p & f
  38.       For Each tb In Cata.Tables
  39.         If tb.Type = "TABLE" Then
  40.           t = Replace(tb.Name, "'", vbNullString)
  41.           If Right(t, 1) = "$" Then
  42.             br = ar
  43.             SQL = "SELECT * FROM [" & s & p & f & "].[" & t & "A6:AA6] WHERE FALSE"
  44.             Set rs = Conn.Execute(SQL)
  45.             For i = 0 To rs.Fields.Count - 1
  46.               n = "`" & rs.Fields(i).Name & "`"
  47.               If dic.Exists(n) Then br(dic(n)) = n
  48.             Next
  49.             br(1) = Replace(br(1), "NULL", "'" & Replace(Split(t, "202")(0), "发货清单,", "") & "'")
  50.             SQL = "SELECT " & Join(br, ",") & " FROM [" & s & p & f & "].[" & t & "A6:AA] WHERE LEN(石种)>0"
  51.             dict.Add SQL, vbNullString
  52.             If dict.Count = 49 Then
  53.               target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
  54.               Set target = Range("A" & Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1)
  55.               dict.RemoveAll
  56.             End If
  57.             'Exit For
  58.           End If
  59.         End If
  60.       Next
  61.     End If
  62.     f = Dir
  63.   Wend
  64.   If dict.Count Then target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
  65.   
  66.   With Range("A1").CurrentRegion
  67.     .Borders.LineStyle = xlContinuous
  68.     .HorizontalAlignment = xlCenter
  69.     .Font.Name = "微软雅黑"
  70.     .Font.Size = 10
  71.     .Value = .Value
  72.   End With
  73.   
  74.   rs.Close
  75.   Set rs = Nothing
  76.   Conn.Close
  77.   Set Conn = Nothing
  78.   Set Cata = Nothing
  79.   Set target = Nothing
  80.   Set dic = Nothing
  81.   Set dict = Nothing
  82.   Application.ScreenUpdating = True
  83.   Beep
  84. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-17 10:13 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-4-17 10:18 | 显示全部楼层
本帖最后由 baofa2 于 2024-4-17 18:23 编辑

需要出问题的附件。

楼下附件……


楼下附件并无不妥测试成功.zip (292.67 KB, 下载次数: 11)

  1. Option Explicit

  2. '哎,随便更改表名及工作表数量…… 适用于 11F 附件

  3. Sub test1() '无花,真不愿占用新楼层,但愿你能看到……
  4.   
  5.   ActiveSheet.UsedRange.Offset(1).Clear
  6.   Application.ScreenUpdating = False
  7.   
  8.   Dim p As String, f As String
  9.   Dim ar, br, dic As Object, dict As Object, target As Range
  10.   Dim Conn As Object, rs As Object, Cata As Object, tb As Object, str_ As String
  11.   Dim strConn As String, SQL As String, s As String, t As String, n As String, i As Long
  12.   
  13.   Set target = Range("A2")
  14.   Set dic = CreateObject("Scripting.Dictionary")
  15.   Set dict = CreateObject("Scripting.Dictionary")
  16.   Set Cata = CreateObject("ADOX.Catalog")
  17.   Set Conn = CreateObject("ADODB.Connection")
  18.   
  19.   ar = Application.Rept(Range("A1").CurrentRegion.Rows(1).Value, 1)
  20.   For i = 1 To UBound(ar)
  21.     s = "`" & Trim(ar(i)) & "`"
  22.     dic.Add s, i
  23.     ar(i) = "NULL AS " & s
  24.   Next
  25.   
  26.   s = "Excel 12.0;HDR=YES;IMEX=1;Database="
  27.   If Application.Version < 12 Then
  28.     s = Replace(s, "12.0", "8.0")
  29.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
  30.   Else
  31.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
  32.   End If
  33.   Conn.Open strConn & ThisWorkbook.FullName
  34.   
  35.   p = ThisWorkbook.Path & "\"
  36.   f = Dir(p & "*.xls*")
  37.   While Len(f)
  38.     If p & f <> ThisWorkbook.FullName Then
  39.       str_ = ""
  40.       If InStr(f, "发货清单,") Then str_ = Replace(Split(f, "202")(0), "发货清单,", "")
  41.       Cata.ActiveConnection = strConn & p & f
  42.       For Each tb In Cata.Tables
  43.         If tb.Type = "TABLE" Then
  44.           t = Replace(tb.Name, "'", vbNullString)
  45.           If Right(t, 1) = "$" Then
  46.             If InStr(t, "发货清单") Then
  47.               br = ar
  48.               SQL = "SELECT * FROM [" & s & p & f & "].[" & t & "A6:AA6] WHERE FALSE"
  49.               Set rs = Conn.Execute(SQL)
  50.               For i = 0 To rs.Fields.Count - 1
  51.                 n = "`" & rs.Fields(i).Name & "`"
  52.                 If dic.Exists(n) Then br(dic(n)) = n
  53.               Next
  54.               If str_ = "" Then str_ = Replace(Split(t, "202")(0), "发货清单,", "")
  55.               br(1) = Replace(br(1), "NULL", "'" & str_ & "'")
  56.               SQL = "SELECT " & Join(br, ",") & " FROM [" & s & p & f & "].[" & t & "A6:AA] WHERE LEN(石种)>0"
  57.               dict.Add SQL, vbNullString
  58.               If dict.Count = 49 Then
  59.                 target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
  60.                 Set target = Range("A" & Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1)
  61.                 dict.RemoveAll
  62.               End If
  63.               'Exit For
  64.             End If
  65.           End If
  66.         End If
  67.       Next
  68.     End If
  69.     f = Dir
  70.   Wend
  71.   If dict.Count Then target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
  72.   
  73.   With Range("A1").CurrentRegion
  74.     .Borders.LineStyle = xlContinuous
  75.     .HorizontalAlignment = xlCenter
  76.     .Font.Name = "微软雅黑"
  77.     .Font.Size = 10
  78.     .Value = .Value
  79.   End With
  80.   
  81.   rs.Close
  82.   Set rs = Nothing
  83.   Conn.Close
  84.   Set Conn = Nothing
  85.   Set Cata = Nothing
  86.   Set target = Nothing
  87.   Set dic = Nothing
  88.   Set dict = Nothing
  89.   Application.ScreenUpdating = True
  90.   Beep
  91. End Sub
复制代码



TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-17 10:25 | 显示全部楼层
我重新上传一下,麻烦各位看看,是不是还是打开很卡。

模板.rar

211.14 KB, 下载次数: 10

TA的精华主题

TA的得分主题

发表于 2024-4-17 10:33 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-4-17 14:07 | 显示全部楼层
我来参与一下,我用工具给你提了一下数据,你看是否可以:
image.jpg

工具中需要设置的参数:
image.jpg

工具介绍: https://club.excelhome.net/threa ... tml?_dsign=e20fcbc2
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-11-17 15:51 , Processed in 0.034384 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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