Excel精英培训网

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

[已解决]求助批量合并数据

[复制链接]
发表于 2012-12-23 03:44 | 显示全部楼层 |阅读模式
各位高手们:我现在需要把文件中的除了汇总之外的表格中的第四列提取到汇总表格中,实际中在在同一个文件夹下的工作薄有上百个,每个表中有将近800*8的数据,自己水平有限,实在是弄不出来,求指点。。。压缩包中并不是实际工作中的文件,只是作为一个演示,谢谢大家。
最佳答案
2012-12-23 13:39
  1. Option Explicit

  2. Sub 提取数据2()
  3. '---------------------------------------------------------------------------------------
  4. ' Procedure : 提取数据
  5. ' Author    : hwc2ycy
  6. ' Date      : 2012/12/23
  7. ' Purpose   :
  8. '---------------------------------------------------------------------------------------

  9.     Dim app As Object                   'object,EXCEL实例
  10.     Dim arr                             '数组
  11.     Dim iRow&                           '数据所在行
  12.     Dim Filename$, Path$                '文件名,路径

  13.     On Error Resume Next
  14.     Application.ScreenUpdating = False  '关闭刷屏

  15.     Path = ThisWorkbook.Path & Application.PathSeparator
  16.    
  17.     '遍在文件
  18.     Filename = Dir(Path & "*.xls*", vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
  19.    
  20.     If Len(Filename) > 0 Then
  21.         Set app = CreateObject("excel.application")         '创建对象
  22.         '避免打开时运行宏
  23.         app.AutomationSecurity = msoAutomationSecurityForceDisable
  24.         app.DisplayAlerts = False                           '不显示错误
  25.     End If
  26.    
  27.     Do While Len(Filename) > 0
  28.         'Debug.Print Filename
  29.         If Not Filename Like "*" & ThisWorkbook.Name & "*" Then   ' 避免打开本工作簿
  30.             'Debug.Print Filename & "已打开"
  31.             With app.Workbooks.Open(Path & Filename, False, True)
  32.                 If Err.Number <> 0 Then MsgBox Filename & "打开失败": Err.Clear: GoTo error
  33.                 With .Worksheets(1)
  34.                     iRow = .Cells(65536, 1).End(xlUp).Row
  35.                     arr = .Range("d1:d" & iRow)    '读取第4列数据
  36.                 End With
  37.                 .Close False
  38.             End With
  39.             iRow = Cells(65536, 1).End(xlUp).Row    '因为是03的格式,所以没有用ROWS.COUNT
  40.             If iRow > 1 Then iRow = iRow + 1
  41.             Range("a" & iRow).Resize(UBound(arr)) = arr    '写入汇总表格
  42.         End If
  43. error:  Filename = Dir()    '遍历
  44.     Loop
  45.    
  46.     app.AutomationSecurity = msoAutomationSecurityByUI
  47.     app.DisplayAlerts = True
  48.     Set app = Nothing
  49.     Application.ScreenUpdating = True
  50.     MsgBox "提取完成"
  51. End Sub
复制代码
重新改了下。

test.rar

18.23 KB, 下载次数: 42

发表于 2012-12-23 10:08 | 显示全部楼层
回复

使用道具 举报

发表于 2012-12-23 11:00 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-12-23 11:05 编辑

汇总.rar (18.71 KB, 下载次数: 30)
回复

使用道具 举报

发表于 2012-12-23 11:02 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-12-23 11:04 编辑
  1. Option Explicit

  2. Sub 提取数据()
  3. '---------------------------------------------------------------------------------------
  4. ' Procedure : 提取数据
  5. ' Author    : hwc2ycy
  6. ' Date      : 2012/12/23
  7. ' Purpose   :
  8. '---------------------------------------------------------------------------------------

  9.     Dim app As New Excel.Application    'app变量
  10.     Dim arr                             '数组
  11.     Dim iRow&                           '数据所在行
  12.     Dim Filename$, Path$                '文件名,路径
  13.    
  14.     On Error Resume Next
  15.     Application.ScreenUpdating = False  '关闭刷屏
  16.    
  17.     '避免打开时运行宏
  18.     app.AutomationSecurity = msoAutomationSecurityForceDisable
  19.     app.DisplayAlerts = False
  20.     Path = ThisWorkbook.Path & Application.PathSeparator
  21.     '遍在文件
  22.     Filename = Dir(Path & "*.xls*", vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
  23.     Do While Len(Filename) > 0
  24.         'Debug.Print Filename
  25.         If Not Filename Like "*" & ThisWorkbook.Name & "*" Then   ' 避免打开本工作簿
  26.             'Debug.Print Filename & "已打开"
  27.             With app.Workbooks.Open(Path & Filename, False, True)
  28.                 If Err.Number <> 0 Then MsgBox Filename & "打开失败": GoTo error
  29.                 With .Worksheets(1)
  30.                     iRow = .Cells(65536, 1).End(xlUp).Row
  31.                     arr = .Range("d1:d" & iRow) '读取第4列数据
  32.                 End With
  33.                 .Close False
  34.             End With
  35.             iRow = Cells(65536, 1).End(xlUp).Row    '因为是03的格式,所以没有用ROWS.COUNT
  36.             If iRow > 1 Then iRow = iRow + 1
  37.             Range("a" & iRow).Resize(UBound(arr)) = arr '写入汇总表格
  38.         End If
  39. error:  Filename = Dir()    '遍历

  40.     Loop

  41.     app.AutomationSecurity = msoAutomationSecurityByUI
  42.     app.DisplayAlerts = True
  43.     Set app = Nothing
  44.     Application.ScreenUpdating = True
  45.     MsgBox "提取完成"
  46. End Sub
复制代码
回复

使用道具 举报

发表于 2012-12-23 13:39 | 显示全部楼层    本楼为最佳答案   
  1. Option Explicit

  2. Sub 提取数据2()
  3. '---------------------------------------------------------------------------------------
  4. ' Procedure : 提取数据
  5. ' Author    : hwc2ycy
  6. ' Date      : 2012/12/23
  7. ' Purpose   :
  8. '---------------------------------------------------------------------------------------

  9.     Dim app As Object                   'object,EXCEL实例
  10.     Dim arr                             '数组
  11.     Dim iRow&                           '数据所在行
  12.     Dim Filename$, Path$                '文件名,路径

  13.     On Error Resume Next
  14.     Application.ScreenUpdating = False  '关闭刷屏

  15.     Path = ThisWorkbook.Path & Application.PathSeparator
  16.    
  17.     '遍在文件
  18.     Filename = Dir(Path & "*.xls*", vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
  19.    
  20.     If Len(Filename) > 0 Then
  21.         Set app = CreateObject("excel.application")         '创建对象
  22.         '避免打开时运行宏
  23.         app.AutomationSecurity = msoAutomationSecurityForceDisable
  24.         app.DisplayAlerts = False                           '不显示错误
  25.     End If
  26.    
  27.     Do While Len(Filename) > 0
  28.         'Debug.Print Filename
  29.         If Not Filename Like "*" & ThisWorkbook.Name & "*" Then   ' 避免打开本工作簿
  30.             'Debug.Print Filename & "已打开"
  31.             With app.Workbooks.Open(Path & Filename, False, True)
  32.                 If Err.Number <> 0 Then MsgBox Filename & "打开失败": Err.Clear: GoTo error
  33.                 With .Worksheets(1)
  34.                     iRow = .Cells(65536, 1).End(xlUp).Row
  35.                     arr = .Range("d1:d" & iRow)    '读取第4列数据
  36.                 End With
  37.                 .Close False
  38.             End With
  39.             iRow = Cells(65536, 1).End(xlUp).Row    '因为是03的格式,所以没有用ROWS.COUNT
  40.             If iRow > 1 Then iRow = iRow + 1
  41.             Range("a" & iRow).Resize(UBound(arr)) = arr    '写入汇总表格
  42.         End If
  43. error:  Filename = Dir()    '遍历
  44.     Loop
  45.    
  46.     app.AutomationSecurity = msoAutomationSecurityByUI
  47.     app.DisplayAlerts = True
  48.     Set app = Nothing
  49.     Application.ScreenUpdating = True
  50.     MsgBox "提取完成"
  51. End Sub
复制代码
重新改了下。
回复

使用道具 举报

 楼主| 发表于 2012-12-23 15:52 | 显示全部楼层
hwc2ycy 发表于 2012-12-23 13:39
重新改了下。

班班好棒啊~我要继续再看一下~~~
回复

使用道具 举报

发表于 2012-12-23 21:26 | 显示全部楼层
  1. Option Explicit

  2. Sub 提取数据3()
  3. '---------------------------------------------------------------------------------------
  4. ' Procedure : 提取数据
  5. ' Author    : hwc2ycy
  6. ' Date      : 2012/12/23
  7. ' Purpose   :
  8. '---------------------------------------------------------------------------------------

  9.     Dim app As Object                   'object,EXCEL实例
  10.     Dim arr                             '数组
  11.     Dim iRow&, iCol&                          '数据所在行
  12.     Dim Filename$, Path$                '文件名,路径

  13.     On Error Resume Next
  14.     Application.ScreenUpdating = False  '关闭刷屏

  15.     Path = ThisWorkbook.Path & Application.PathSeparator
  16.    
  17.     '遍在文件
  18.     Filename = Dir(Path & "*.xls*", vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
  19.    
  20.     If Len(Filename) > 0 Then
  21.         Set app = CreateObject("excel.application")         '创建对象
  22.         '避免打开时运行宏
  23.         app.AutomationSecurity = msoAutomationSecurityForceDisable
  24.         app.DisplayAlerts = False                           '不显示错误
  25.     End If
  26.    
  27.     Do While Len(Filename) > 0
  28.         'Debug.Print Filename
  29.         If Not Filename Like "*" & ThisWorkbook.Name & "*" Then   ' 避免打开本工作簿
  30.             'Debug.Print Filename & "已打开"
  31.             With app.Workbooks.Open(Path & Filename, False, True)
  32.                 If Err.Number <> 0 Then MsgBox Filename & "打开失败": Err.Clear: GoTo error
  33.                 iCol = iCol + 1
  34.                 With .Worksheets(1)
  35.                     iRow = .Cells(65536, 1).End(xlUp).Row
  36.                     arr = .Range("d1:d" & iRow)    '读取第4列数据
  37.                 End With
  38.                 .Close False
  39.             End With
  40.             iRow = Cells(65536, iCol).End(xlUp).Row    '因为是03的格式,所以没有用ROWS.COUNT
  41.             If iRow > 1 Then iRow = iRow + 1
  42.             Cells(iRow, iCol).Resize(UBound(arr)) = arr   '写入汇总表格
  43.         End If
  44. error:  Filename = Dir()    '遍历
  45.     Loop
  46.    
  47.     app.AutomationSecurity = msoAutomationSecurityByUI
  48.     app.DisplayAlerts = True
  49.     Set app = Nothing
  50.     Application.ScreenUpdating = True
  51.     MsgBox "提取完成"
  52. End Sub
复制代码
分列存数据。
回复

使用道具 举报

发表于 2013-3-23 22:45 | 显示全部楼层
07mickey 发表于 2012-12-23 15:52
班班好棒啊~我要继续再看一下~~~

好多个语句,我应该看哪个为准呢?能发一个给我吗?
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 22:28 , Processed in 0.313512 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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