Excel精英培训网

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

[已解决]请教如何汇总文件夹下所有工作簿的名称以及第一个表格的某个单元格数值

[复制链接]
发表于 2014-5-12 14:34 | 显示全部楼层 |阅读模式
请问汇总文件夹下所有工作簿的名称以及第一个表格的某个单元格数值 代码怎么写
最佳答案
2014-5-12 15:07
  1. Sub demo()
  2.     Dim strPath As String, strFile As String
  3.     Dim arr(1 To 1000, 1 To 3)
  4.     Dim i As Long
  5.     strPath = ThisWorkbook.Path & Application.PathSeparator & "源文件" & Application.PathSeparator
  6.     strFile = Dir(strPath & "*.xls")
  7.     Application.ScreenUpdating = False
  8.     Do While Len(strFile)
  9.         If strFile <> ThisWorkbook.Name Then
  10.             With GetObject(strPath & strFile)
  11.                 Windows(.Name).Visible = True
  12.                 i = i + 1
  13.                 arr(i, 1) = Left(strFile, InStrRev(strFile, ".") - 1)
  14.                 With .Worksheets(1)
  15.                     arr(i, 2) = .Range("e2").Value
  16.                     arr(i, 3) = .Range("f2").Value
  17.                 End With
  18.                 .Close False
  19.             End With
  20.         End If
  21.         strFile = Dir
  22.     Loop
  23.     If i > 0 Then
  24.         With Cells(Rows.Count, 1).End(xlUp).Offset(1)
  25.             .Resize(i, 3).Value = arr
  26.         End With
  27.         MsgBox "汇总完成"
  28.     Else
  29.         MsgBox "当前数据目录下无文件可汇总"
  30.     End If
  31.     Application.ScreenUpdating = True
  32. End Sub
复制代码

提问.rar

19.69 KB, 下载次数: 13

发表于 2014-5-12 15:03 | 显示全部楼层
  1. Sub demo()
  2.     Dim strPath As String, strFile As String
  3.     Dim arr(1 To 1000, 1 To 3)
  4.     Dim i As Long
  5.     strPath = ThisWorkbook.Path & Application.PathSeparator & "源文件" & Application.PathSeparator
  6.     strFile = Dir(strPath & "*.xls")
  7.     Application.ScreenUpdating = False
  8.     Do While Len(strFile)
  9.         If strFile <> ThisWorkbook.Name Then
  10.             With GetObject(strPath & strFile)
  11.                 Windows(.Name).Visible = True
  12.                 i = i + 1
  13.                 arr(i, 1) = Left(strFile, InStrRev(strFile, ".") - 1)
  14.                 With .Worksheets(1)
  15.                     arr(i, 2) = .Range("e2").Value
  16.                     arr(i, 3) = .Range("f2").Value
  17.                 End With
  18.                 .Close False
  19.             End With
  20.         End If
  21.         strFile = Dir
  22.     Loop
  23.     With Cells(Rows.Count, 1).End(xlUp).Offset(1)
  24.         .Resize(i, 3).Value = arr
  25.     End With
  26.     Application.ScreenUpdating = True
  27.     MsgBox "ok"
  28. End Sub
复制代码
回复

使用道具 举报

发表于 2014-5-12 15:06 | 显示全部楼层
后面忘了加判断了,避免找不到文件的情况。
回复

使用道具 举报

发表于 2014-5-12 15:07 | 显示全部楼层    本楼为最佳答案   
  1. Sub demo()
  2.     Dim strPath As String, strFile As String
  3.     Dim arr(1 To 1000, 1 To 3)
  4.     Dim i As Long
  5.     strPath = ThisWorkbook.Path & Application.PathSeparator & "源文件" & Application.PathSeparator
  6.     strFile = Dir(strPath & "*.xls")
  7.     Application.ScreenUpdating = False
  8.     Do While Len(strFile)
  9.         If strFile <> ThisWorkbook.Name Then
  10.             With GetObject(strPath & strFile)
  11.                 Windows(.Name).Visible = True
  12.                 i = i + 1
  13.                 arr(i, 1) = Left(strFile, InStrRev(strFile, ".") - 1)
  14.                 With .Worksheets(1)
  15.                     arr(i, 2) = .Range("e2").Value
  16.                     arr(i, 3) = .Range("f2").Value
  17.                 End With
  18.                 .Close False
  19.             End With
  20.         End If
  21.         strFile = Dir
  22.     Loop
  23.     If i > 0 Then
  24.         With Cells(Rows.Count, 1).End(xlUp).Offset(1)
  25.             .Resize(i, 3).Value = arr
  26.         End With
  27.         MsgBox "汇总完成"
  28.     Else
  29.         MsgBox "当前数据目录下无文件可汇总"
  30.     End If
  31.     Application.ScreenUpdating = True
  32. End Sub
复制代码
回复

使用道具 举报

发表于 2014-5-12 15:08 | 显示全部楼层
汇总.rar (17.53 KB, 下载次数: 4)
回复

使用道具 举报

 楼主| 发表于 2014-5-12 16:49 | 显示全部楼层
hwc2ycy 发表于 2014-5-12 15:08

如果E2  F2单元格是个公式  汇总过来的是个#REF!,怎样修改那个代码?
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 11:33 , Processed in 0.302851 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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