Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2288|回复: 4

[已解决]如何合并同一文件夹下的工作簿和工作表中的表格?

[复制链接]
发表于 2016-11-18 14:19 | 显示全部楼层 |阅读模式
如何合并同一文件夹下的所有工作簿和工作表中的表格?

如何使代码遍历文件夹中的所有工作簿和所有工作簿中的所有工作表,使他们合并到一个工作表中?

代码如何写?谢谢!


abc.rar (12.35 KB, 下载次数: 16)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2016-11-18 14:22 | 显示全部楼层
  1.     Sub GetDatas()
  2.       
  3.         Dim strThisPath As String
  4.         Dim strThisName As String
  5.       
  6.         Dim iMaxCount_0 As Long
  7.         Dim Rows_0      As Long
  8.         Dim Cns_0       As Long
  9.         Dim KeyRows     As Long
  10.         Dim i As Long, ia As Long, ib As Long, ic As Long
  11.       
  12.         Dim cfg_0       As Variant
  13.         Dim cfg_1       As Variant
  14.         Dim cfgRange_0  As Variant
  15.       
  16.         Dim isFind      As Boolean
  17.       
  18.         Dim objFile
  19.         Dim objExcel
  20.       
  21.         strThisPath = ThisWorkbook.Path & ""
  22.         strThisName = ThisWorkbook.Name
  23.         objFile = Dir(strThisPath & "*.xlsx")

  24.         Rows_0 = 2
  25.         Cns_0 = 1
  26.         iMaxCount_0 = 100       '汇总表最大标题数量,当需求超出可自行调整,影响运行效率,值越大,运行会越慢
  27.         Set cfgRange_0 = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(65536, iMaxCount_0))
  28.         cfgRange_0.ClearContents
  29.         cfg_0 = cfgRange_0.Value
  30.       
  31.         Do While objFile <> ""
  32.             If objFile <> strThisName Then
  33.                 Set objExcel = Workbooks.Open(strThisPath & objFile)
  34.                 cfg_1 = objExcel.Sheets(1).[a1].CurrentRegion
  35.                
  36.                 For i = 1 To UBound(cfg_1)
  37.                     If cfg_1(i, 1) = "序号" Then
  38.                         KeyRows = i
  39.                         Exit For
  40.                     End If
  41.                 Next i
  42.                
  43.                 For i = 1 To UBound(cfg_1, 2)
  44.                     isFind = False
  45.                     For ia = 1 To UBound(cfg_0, 2)
  46.                         If Len(cfg_0(1, ia)) = 0 Then Exit For
  47.                         If cfg_1(KeyRows, i) = cfg_0(1, ia) Then
  48.                             isFind = True
  49.                             Exit For
  50.                         End If
  51.                     Next ia
  52.                     If isFind = False Then
  53.                         cfg_0(1, Cns_0) = cfg_1(KeyRows, i)
  54.                         Cns_0 = Cns_0 + 1
  55.                     End If
  56.                 Next i
  57.                
  58.                 For ia = KeyRows + 1 To UBound(cfg_1)
  59.                     For ib = 1 To iMaxCount_0
  60.                         For ic = 1 To UBound(cfg_1, 2)
  61.                             If cfg_0(1, ib) = cfg_1(KeyRows, ic) Then
  62.                                 cfg_0(Rows_0, ib) = cfg_1(ia, ic)
  63.                                 Exit For
  64.                             End If
  65.                         Next ic
  66.                     Next ib
  67.                     Rows_0 = Rows_0 + 1
  68.                 Next ia
  69.                 objExcel.Close True
  70.             End If
  71.             objFile = Dir
  72.         Loop
  73.       
  74.         cfgRange_0.Value = cfg_0
  75.     End Sub
复制代码


这个代码只能合并同一文件夹中的工作簿,不能扫描工作簿中的每一个工作表,代码要如何改写?谢谢!
回复

使用道具 举报

发表于 2016-11-18 14:47 | 显示全部楼层    本楼为最佳答案   
就是扫读一个工作簿中的所有工作表,对吗?
但你的附件太简单了,红字是我改的:

        Do While objFile <> ""
            If objFile <> strThisName Then
                Set objExcel = Workbooks.Open(strThisPath & objFile)
                For Each sh In objExcel.Sheets
                    cfg_1 = sh.[a1].CurrentRegion
                    
                    For i = 1 To UBound(cfg_1)
                        If cfg_1(i, 1) = "序号" Then
                            KeyRows = i
                            Exit For
                        End If
                    Next i
                    
                    For i = 1 To UBound(cfg_1, 2)
                        isFind = False
                        For ia = 1 To UBound(cfg_0, 2)
                            If Len(cfg_0(1, ia)) = 0 Then Exit For
                            If cfg_1(KeyRows, i) = cfg_0(1, ia) Then
                                isFind = True
                                Exit For
                            End If
                        Next ia
                        If isFind = False Then
                            cfg_0(1, Cns_0) = cfg_1(KeyRows, i)
                            Cns_0 = Cns_0 + 1
                        End If
                    Next i
                    
                    For ia = KeyRows + 1 To UBound(cfg_1)
                        For ib = 1 To iMaxCount_0
                            For ic = 1 To UBound(cfg_1, 2)
                                If cfg_0(1, ib) = cfg_1(KeyRows, ic) Then
                                    cfg_0(Rows_0, ib) = cfg_1(ia, ic)
                                    Exit For
                                End If
                            Next ic
                        Next ib
                        Rows_0 = Rows_0 + 1
                    Next ia
                Next
                objExcel.Close True
            End If
            objFile = Dir
        Loop

评分

参与人数 1 +1 收起 理由
FIFAWORLDCUP + 1 很给力

查看全部评分

回复

使用道具 举报

发表于 2016-11-18 16:04 | 显示全部楼层
本帖最后由 su45 于 2016-11-18 16:12 编辑

解决了吗?解决了就设置为最佳
回复

使用道具 举报

 楼主| 发表于 2016-11-18 16:15 | 显示全部楼层
su45 发表于 2016-11-18 16:04
解决了吗?解决了就设置为最佳

谢谢高手!
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-6 10:05 , Processed in 0.318509 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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