ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 汇总多个表格数据

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-4-23 20:44 | 显示全部楼层
参与一下。。

汇总工作表.zip

59.55 KB, 下载次数: 21

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-4-23 20:45 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub 汇总()
  2. Dim arr, j, r, c
  3. c = 21
  4. Application.ScreenUpdating = False
  5. Application.DisplayAlerts = False
  6. ReDim arr(1 To c)
  7. Set fso = CreateObject("scripting.filesystemobject")
  8. p = ThisWorkbook.Path & "\测试"
  9. For Each f In fso.getfolder(p).Files
  10.     r = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
  11.     Set wb = Workbooks.Open(f)
  12.     Set sht = wb.Sheets(1)
  13.     With sht
  14.         arr(1) = r - 1
  15.         arr(2) = .[i3]
  16.         arr(3) = .[i4]
  17.         arr(4) = .[c5]
  18.         arr(5) = .[i5]
  19.         arr(6) = .[i6]
  20.         arr(7) = .[a9]
  21.         arr(8) = .[e9]
  22.         arr(9) = .[i9]
  23.         For j = 1 To 12
  24.             arr(j + 9) = .Cells(12, j)
  25.         Next
  26.     End With
  27.     wb.Close False
  28.     Sheet1.Cells(r + 1, 1).Resize(1, c) = arr
  29. Next
  30. Sheet1.Range("b:b,d:e,h:h").WrapText = True
  31. Sheet1.Range("c:c,f:f").NumberFormatLocal = "yyyy-m-d"
  32. Application.ScreenUpdating = True
  33. Application.DisplayAlerts = True
  34. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2024-4-24 15:21 | 显示全部楼层
  1. Sub test1() '参与练习一下 https://club.excelhome.net/thread-1690486-1-1.html
  2.   
  3.   Dim results(), ar, br(1), cel As Range, Item_
  4.   Dim i As Long, j As Long, pos As Long, cnt As Long
  5.   Dim Conn As Object, re As Object, dict As Object
  6.   Dim SQL As String, s As String, p As String, f As String
  7.   
  8.   Set dict = CreateObject("Scripting.Dictionary")
  9.   Set re = CreateObject("VBScript.RegExp")
  10.   re.Global = True
  11.   re.Pattern = "(\D+)(\d+)(\D+)(\d+)"
  12.   
  13.   cnt = 1
  14.   ar = Split("I3 I4 C5 I5 I6 A9 E9 I9 A12:L12")
  15.   For Each Item_ In ar
  16.     For Each cel In Range(Item_)
  17.       i = 0
  18.       s = cel.Address(, , xlR1C1)
  19.       For j = 4 To 2 Step -2
  20.         br(i) = re.Replace(s, "$" & j) - 1
  21.         i = i + 1
  22.       Next
  23.       cnt = cnt + 1
  24.       dict.Add Join(br, "|"), cnt
  25.     Next
  26.   Next
  27.   ReDim results(1 To 5000, 1 To cnt)
  28.   Set re = Nothing
  29.   
  30.   cnt = 0
  31.   ActiveSheet.UsedRange.Offset(2).ClearContents
  32.   Application.ScreenUpdating = False
  33.   
  34.   Set Conn = CreateObject("ADODB.Connection")
  35.   Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
  36.   
  37.   p = ThisWorkbook.Path & "\测试\"
  38.   f = Dir(p & "*.xls*")
  39.   Do
  40.     If f <> ThisWorkbook.Name Then
  41.       cnt = cnt + 1
  42.       results(cnt, 1) = cnt
  43.       SQL = "SELECT * FROM [Excel 12.0;HDR=NO;IMEX=1;Database=" & p & f & "].[$A1:M12]"
  44.       ar = Conn.Execute(SQL).GetRows
  45.       For j = 0 To UBound(ar, 2)
  46.         For i = 0 To UBound(ar)
  47.           s = i & "|" & j
  48.           If dict.Exists(s) Then
  49.             pos = dict(s)
  50.             results(cnt, pos) = ar(i, j)
  51.           End If
  52.         Next
  53.       Next
  54.     End If
  55.     f = Dir
  56.   Loop While f <> ""
  57.   
  58.   Range("A3").Resize(cnt, UBound(results, 2)) = results
  59.   
  60.   Conn.Close
  61.   Set Conn = Nothing
  62.   Set dict = Nothing
  63.   Application.ScreenUpdating = True
  64.   Beep
  65. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2024-4-24 16:54 | 显示全部楼层
参与一下,是有点麻烦的。

汇总工作表.7z

24.6 KB, 下载次数: 8

TA的精华主题

TA的得分主题

发表于 2024-4-24 16:55 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
参与一下。。。

  1. Sub ykcbf() '//2024.4.24
  2.     Set fso = CreateObject("scripting.filesystemobject")
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     Application.ScreenUpdating = False
  5.     Set sh = ThisWorkbook.Sheets("Sheet1")
  6.     With sh
  7.         c = .UsedRange.Columns.Count
  8.         For j = 2 To c
  9.             s = .Cells(2, j)
  10.             d(s) = j
  11.         Next
  12.     End With
  13.     p = ThisWorkbook.Path & "\测试"
  14.     a = [{"G3","G4","A5","G5","G6","A8","E8","H8"}]
  15.     b = [{"i3","i4","c5","i5","i6","A9","e9","i9"}]
  16.     ReDim brr(1 To 1000, 1 To c)
  17.     On Error Resume Next
  18.     For Each f In fso.GetFolder(p).Files
  19.         If f.Name Like "*.xls*" Then
  20.             fn = fso.GetBaseName(f)
  21.             m = m + 1
  22.             brr(m, 1) = m
  23.             Set wb = Workbooks.Open(f, 0)
  24.             With wb.Sheets(1)
  25.                 arr = .UsedRange
  26.                 For x = 1 To UBound(a)
  27.                     s = Trim(.Range(a(x)).Value)
  28.                     If d.exists(s) Then
  29.                         brr(m, d(s)) = .Range(b(x)).Value
  30.                     End If
  31.                 Next
  32.             End With
  33.             wb.Close False
  34.             For j = 1 To UBound(arr, 2)
  35.                 s = arr(11, j)
  36.                 If d.exists(s) Then
  37.                     brr(m, d(s)) = arr(12, j)
  38.                 End If
  39.             Next
  40.         End If
  41.     Next f
  42.     With sh
  43.         .UsedRange.Offset(2).Clear
  44.         With .[a3].Resize(m, c)
  45.             .Value = brr
  46.             .Borders.LineStyle = 1
  47.             .HorizontalAlignment = xlCenter
  48.             .VerticalAlignment = xlCenter
  49.             .WrapText = True
  50.         End With
  51.     End With
  52.     Set d = Nothing
  53.     Application.ScreenUpdating = True
  54.     MsgBox "OK!"
  55. End Sub
复制代码


评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-4-24 17:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub test1() '改进一下,通用Excel和WPS,另添加一些格式
  2.   
  3.   Dim results(), ar, br(1), cel As Range, Item_
  4.   Dim i As Long, j As Long, pos As Long, cnt As Long
  5.   Dim Conn As Object, re As Object, dict As Object, Flag As Boolean
  6.   Dim strConn As String, SQL As String, s As String, p As String, f As String
  7.   
  8.   Set Conn = CreateObject("ADODB.Connection")
  9.   Set dict = CreateObject("Scripting.Dictionary")
  10.   Set re = CreateObject("VBScript.RegExp")
  11.   re.Global = True
  12.   re.Pattern = "(\D+)(\d+)(\D+)(\d+)"
  13.   
  14.   ar = Split("I3 I4 C5 I5 I6 A9 E9 I9 A12:L12")
  15.   For Each Item_ In ar
  16.     For Each cel In Range(Item_)
  17.       s = cel.Address(, , xlR1C1)
  18.       For j = 4 To 2 Step -2
  19.         br(-CInt(j = 2)) = re.Replace(s, "$" & j) - 1
  20.       Next
  21.       dict.Add Join(br, "|"), dict.Count + 2
  22.     Next
  23.   Next
  24.   ReDim results(1 To 2345, 1 To dict.Count + 1)
  25.   Set re = Nothing
  26.   
  27.   ActiveSheet.UsedRange.Offset(2).Clear 'Contents
  28.   Application.ScreenUpdating = False
  29.   
  30.   Flag = InStr(Application.Path, "WPS") > 0
  31.   s = "Excel 12.0;HDR=NO;IMEX=1;Database="
  32.   If Application.Version < 12 Or Flag Then
  33.     s = Replace(s, "12.0", "8.0")
  34.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=NO;IMEX=1';Data Source="
  35.   Else
  36.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=NO;IMEX=1';Data Source="
  37.   End If
  38.   'Conn.Open strConn & ThisWorkbook.FullName
  39.   
  40.   p = ThisWorkbook.Path & "\测试\"
  41.   SQL = "SELECT * FROM [" & s & p & "[.f]].[$A1:M12]"
  42.   f = Dir(p & "*.xls*")
  43.   Do
  44.     If f <> ThisWorkbook.Name Then
  45.       cnt = cnt + 1
  46.       results(cnt, 1) = cnt
  47.       If cnt = 1 Then Conn.Open strConn & p & f
  48.       ar = Conn.Execute(Replace(SQL, "[.f]", f)).GetRows
  49.       For j = 0 To UBound(ar, 2)
  50.         For i = 0 To UBound(ar)
  51.           s = i & "|" & j
  52.           If dict.Exists(s) Then
  53.             pos = dict(s)
  54.             results(cnt, pos) = ar(i, j)
  55.           End If
  56.         Next
  57.       Next
  58.     End If
  59.     f = Dir
  60.   Loop While f <> ""
  61.   
  62.   Range("A3").Resize(cnt, UBound(results, 2)) = results
  63.   With Range("A1").CurrentRegion
  64.     With Intersect(.Offset(0), .Offset(2))
  65.       .Borders.LineStyle = xlContinuous
  66.       .HorizontalAlignment = xlCenter
  67.       .Font.Size = 9
  68.     End With
  69.   End With
  70.   
  71.   Conn.Close
  72.   Set Conn = Nothing
  73.   Set dict = Nothing
  74.   Application.ScreenUpdating = True
  75.   Beep
  76. End Sub
复制代码

评分

1

查看全部评分

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

本版积分规则

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

GMT+8, 2024-5-7 07:03 , Processed in 0.033523 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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