ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

汇总同一文件夹中的工作簿

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-5 18:38 | 显示全部楼层 |阅读模式
利用汇总表头的字段汇总同一个文件夹内的各个工作簿中各个工作表对应数据,请大神指导,谢谢。

汇总同一文件夹中的工作簿.rar

24.48 KB, 下载次数: 15

TA的精华主题

TA的得分主题

发表于 2024-5-5 19:23 | 显示全部楼层
Option Explicit
Sub test()
    Dim strFileName$, strPath$, wks As Worksheet
    Dim ar, strResult$(), i&, j&, r&, dic As Object, iPosCol&
   
    Application.ScreenUpdating = False
    Set dic = CreateObject("Scripting.Dictionary")
   
    ar = Range("A2:G2").Value
    ReDim strResult(1 To 10 ^ 5, 1 To UBound(ar, 2))
    r = 1
    For j = 1 To UBound(ar, 2)
        dic(ar(1, j)) = j
        strResult(r, j) = ar(1, j)
    Next j
   
    strPath = ThisWorkbook.Path & "\"
    strFileName = Dir(strPath & "*.xls*")
    Do Until strFileName = ""
        If strFileName <> ThisWorkbook.Name Then
            With GetObject(strPath & strFileName)
                For Each wks In .Worksheets
                    If wks.[B2].Value = "单号" Then
                        ar = wks.[A1].CurrentRegion.Value
                        For i = 3 To UBound(ar)
                            r = r + 1
                            For j = 1 To UBound(ar, 2)
                                If dic.exists(ar(2, j)) Then
                                    iPosCol = dic(ar(2, j))
                                    strResult(r, iPosCol) = ar(i, j)
                                End If
                            Next j
                        Next i
                    End If
                Next
                .Close False
            End With
        End If
        strFileName = Dir
    Loop
   
    With [A2].Resize(r, UBound(strResult, 2))
        .Clear
        .Value = strResult
        .HorizontalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous
        .EntireColumn.AutoFit
        .EntireRow.AutoFit
    End With
    Set wks = Nothing
    Application.ScreenUpdating = True
    Beep
End Sub

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-5-5 19:25 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-5-5 20:02 | 显示全部楼层
本帖最后由 ykcbf1100 于 2024-5-5 20:56 编辑

刚才代码有误,更新一下。

汇总同一文件夹中的工作簿.7z

37.93 KB, 下载次数: 20

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-5-5 20:03 | 显示全部楼层
本帖最后由 ykcbf1100 于 2024-5-5 20:57 编辑

参与一下。。。

  1. Sub ykcbf()  '//2024.5.5
  2.     Set fso = CreateObject("scripting.filesystemobject")
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     Application.ScreenUpdating = False
  5.     Set sh = ThisWorkbook.Sheets("汇总表")
  6.     With sh
  7.         For j = 1 To 7
  8.             s = .Cells(2, j)
  9.             d(s) = j
  10.         Next
  11.     End With
  12.     p = ThisWorkbook.Path & ""
  13.     ReDim brr(1 To 10000, 1 To 10)
  14.     For Each f In fso.GetFolder(p).Files
  15.         If f.Name Like "*.xls*" Then
  16.             If InStr(f.Name, ThisWorkbook.Name) = 0 Then
  17.                 fn = fso.GetBaseName(f)
  18.                 Set wb = Workbooks.Open(f, 0)
  19.                 For Each sht In wb.Sheets
  20.                     If sht.[b2].Value = "单号" Then
  21.                         With sht
  22.                             r = .Cells(Rows.Count, 2).End(3).Row
  23.                             arr = .[a1].Resize(r, 8)
  24.                         End With
  25.                         For i = 3 To UBound(arr)
  26.                             If arr(i, 2) <> Empty Then
  27.                                 M = M + 1
  28.                                 For j = 1 To UBound(arr, 2)
  29.                                     s = arr(2, j)
  30.                                     If d.exists(s) Then
  31.                                         brr(M, d(s)) = arr(i, j)
  32.                                     End If
  33.                                 Next
  34.                             End If
  35.                         Next
  36.                     End If
  37.                 Next
  38.                 wb.Close False
  39.             End If
  40.         End If
  41.     Next f
  42.     On Error Resume Next
  43.     With sh
  44.         .[a3:g10000].Clear
  45.         .[a2].Resize(1, 7).Interior.Color = 49407
  46.         .Columns("e:g").NumberFormatLocal = "@"
  47.         With .[a3].Resize(M, 7)
  48.             .Value = brr
  49.             .Borders.LineStyle = 1
  50.             .HorizontalAlignment = xlCenter
  51.             .VerticalAlignment = xlCenter
  52.         End With
  53.     End With
  54.     Set d = Nothing
  55.     Application.ScreenUpdating = True
  56.     MsgBox "OK!"
  57. End Sub
复制代码


评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-5-5 21:53 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Sub sd汇总()
  2. Dim arr, brr, i, j, r, d, sht
  3. Set d = CreateObject("scripting.dictionary")
  4. Set fso = CreateObject("scripting.filesystemobject")
  5. p = ThisWorkbook.Path & ""
  6. crr = Sheets("汇总表").Range("a2").CurrentRegion
  7. For i = 1 To UBound(crr, 2)
  8.     If crr(1, i) <> "" Then
  9.         sa = crr(1, i)
  10.         d(sa) = i
  11.     End If
  12. Next
  13. ReDim brr(1 To 100000, 1 To 7)
  14. For Each f In fso.getfolder(p).Files
  15.     If f.Name <> ThisWorkbook.Name Then
  16.         Set wb = Workbooks.Open(f)
  17.         For Each sh In Sheets
  18.             Debug.Print sh.Name
  19.             If sh.[b2] = "单号" Then
  20.                 arr = sh.UsedRange
  21.                 For i = 3 To UBound(arr)
  22.                     If arr(i, 2) & arr(i, 3) <> Empty Then
  23.                         n = n + 1
  24.                         For j = 1 To UBound(arr, 2)
  25.                             sb = arr(2, j)
  26.                             If d.exists(sb) Then
  27.                                 brr(n, d(sb)) = arr(i, j)
  28.                             End If
  29.                         Next
  30.                     End If
  31.                 Next
  32.             End If
  33.         Next
  34.         wb.Close False
  35.     End If
  36. Next
  37. With Sheet1
  38. .[g:g].NumberFormatLocal = "@"
  39. .[a3].Resize(n, 7) = brr
  40. End With
  41. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2024-5-5 21:54 | 显示全部楼层
请测试。。

汇总同一文件夹中的工作簿.zip

37.49 KB, 下载次数: 15

TA的精华主题

TA的得分主题

发表于 2024-5-6 09:13 | 显示全部楼层

你这For Each sh In Sheets应该是For Each sh In wb.Sheets吧。
你这代码有没有验证过啊。

TA的精华主题

TA的得分主题

发表于 2024-5-6 12:32 | 显示全部楼层
  1. Sub test1() '参与练习 ……

  2.   Sheet1.Activate
  3.   ActiveSheet.UsedRange.Offset(2).ClearContents
  4.   Application.ScreenUpdating = False
  5.   
  6.   Dim strPath  As String, strFile 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, Field As String
  10.   Dim s As String, t As String, i As Long
  11.   
  12.   Set target = Range("A3")
  13.   Set dict = CreateObject("Scripting.Dictionary")
  14.   Set dic = CreateObject("Scripting.Dictionary")
  15.   Set Cata = CreateObject("ADOX.Catalog")
  16.   Set Conn = CreateObject("ADODB.Connection")
  17.   
  18.   ar = Application.Rept(Range("A1").CurrentRegion.Rows(2).Value, 1)
  19.   For i = 1 To UBound(ar)
  20.     Field = "`" & ar(i) & "`"
  21.     dic.Add Field, i
  22.     ar(i) = "'' AS " & Field
  23.   Next
  24.   
  25.   s = "Excel 12.0;HDR=YES;IMEX=1;Database="
  26.   If Application.Version < 12 Or InStr(Application.Path, "WPS") > 0 Then
  27.     s = Replace(s, "12.0", "8.0")
  28.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
  29.   Else
  30.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
  31.   End If
  32.   
  33.   strPath = ThisWorkbook.Path & "\"
  34.   strFile = Dir(strPath & "*.xls*")
  35.   While Len(strFile)
  36.     If strPath & strFile <> ThisWorkbook.FullName Then
  37.       If Conn.State <> 1 Then
  38.         Conn.Open strConn & strPath & strFile
  39.         Cata.ActiveConnection = Conn
  40.       Else
  41.         Cata.ActiveConnection = strConn & strPath & strFile
  42.       End If
  43.       For Each tb In Cata.Tables
  44.         If tb.Type = "TABLE" Then
  45.           t = Replace(tb.Name, "'", vbNullString)
  46.           If Right(t, 1) = "$" Then
  47.             SQL = "SELECT * FROM [" & s & strPath & strFile & "].[" & t & "A2:Z] WHERE FALSE"
  48.             Set rs = Conn.Execute(SQL)
  49.             If rs.Fields(1).Name = "单号" Then
  50.               br = ar
  51.               For i = 0 To rs.Fields.Count - 1
  52.                 Field = "`" & rs.Fields(i).Name & "`"
  53.                 If dic.Exists(Field) Then br(dic(Field)) = Field
  54.               Next
  55.               SQL = "SELECT " & Join(br, ",") & " FROM [" & s & strPath & strFile & "].[" & t & "A2:Z] WHERE LEN(单号)>0 OR LEN(品名)>0"
  56.               dict.Add SQL, vbNullString
  57.               If dict.Count = 49 Then
  58.                 target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
  59.                 Set target = Range("A" & Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1)
  60.                 dict.RemoveAll
  61.               End If
  62.             End If
  63.           End If
  64.         End If
  65.       Next
  66.     End If
  67.     strFile = Dir
  68.   Wend
  69.   If dict.Count Then target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
  70.   
  71.   Set target = Nothing
  72.   Set Cata = Nothing
  73.   rs.Close: Set rs = Nothing
  74.   Conn.Close: Set Conn = Nothing
  75.   Set dic = Nothing: Set dict = Nothing
  76.   Application.ScreenUpdating = True
  77.   Beep
  78. End Sub
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-19 06:56 , Processed in 0.049102 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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