Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: yuan1987

[已解决]vba遍历文件夹选取数据

[复制链接]
 楼主| 发表于 2013-6-28 08:57 | 显示全部楼层
hwc2ycy 发表于 2013-6-28 08:34
没有这个工作表,那就肯定要报错了。

有这个工作表啊,可以下载一下附件啊
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2013-6-28 09:00 | 显示全部楼层    本楼为最佳答案   
  1. Sub 汇总()
  2.     Application.ScreenUpdating = False
  3.     Dim lj As String, m, n
  4.     Dim dirname As String
  5.     Dim nm As String
  6.     Dim wb As Workbook
  7.     Dim i As Integer

  8.     On Error Resume Next

  9.     With Application
  10.         .ScreenUpdating = False
  11.         .DisplayAlerts = False
  12.         .EnableEvents = False
  13.         .Calculation = xlCalculationManual
  14.     End With

  15.     lj = ThisWorkbook.Path
  16.     nm = ThisWorkbook.Name
  17.     dirname = Dir(lj & "\*.xlsx")
  18.     Cells.Clear
  19.     Do While dirname <> ""
  20.         If dirname <> nm Then
  21.             Set wb = Workbooks.Open(Filename:=lj & "" & dirname, UpdateLinks:=False, ReadOnly:=True)
  22.             If Not wb Is Nothing Then
  23.                 With wb
  24.                     If Len(.Sheets("开始").Name) = 0 Then
  25.                     Else
  26.                         i = .Sheets("开始").Range("A65536").End(xlUp).Row
  27.                         ThisWorkbook.Sheets("统计表").Cells(m, 1) = .Sheets("开始").Cells(2, 1).Value
  28.                         ThisWorkbook.Sheets("统计表").Cells(n, 2) = .Sheets("开始").Cells(2, 1).Value
  29.                     End If
  30.                     .Close False
  31.                 End With
  32.                 Set wb = Nothing
  33.             End If
  34.         End If
  35.         dirname = Dir
  36.     Loop
  37.     With Application
  38.         .ScreenUpdating = True
  39.         .DisplayAlerts = True
  40.         .EnableEvents = True
  41.         .Calculation = xlCalculationAutomatic
  42.     End With
  43.     MsgBox "OK"
  44. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-28 09:03 | 显示全部楼层
你的代码不能实现遍历子文件夹。
回复

使用道具 举报

发表于 2013-6-28 09:08 | 显示全部楼层
提供利用FSO+递归的方案供参考!
  1. Dim ARR$(1 To 10000, 1 To 2), i&
  2. Dim Fso As Object

  3. Sub GetPathdata()
  4.   On Error Resume Next
  5.   Application.ScreenUpdating = False
  6.   
  7.   Sheets("统计表").Select
  8.   Set Fso = VBA.CreateObject("Scripting.FileSystemObject")
  9.     Outputfiles ThisWorkbook.Path
  10.   Range("A2").Resize(i, 2) = ARR
  11.   
  12.   Set Fso = Nothing
  13.   Erase ARR
  14.   i = 0
  15.   
  16.   Application.ScreenUpdating = True
  17. End Sub

  18. Sub Outputfiles(Strpath$)
  19.   Dim fd, f, sfd
  20.   Dim N&
  21.   Dim Wb As Workbook
  22.   
  23.   Set fd = Fso.GetFolder(Strpath)
  24.   
  25.   For Each f In fd.Files
  26.       If f.Name Like "*基本情况表.xls*" Then
  27.          Set Wb = Workbooks.Open(Filename:=f.Path)
  28.          i = i + 1
  29.          With Wb.Sheets(1)
  30.            ARR(i, 1) = .Range("a2")
  31.            ARR(i, 2) = .Range("b2")
  32.          End With
  33.         Wb.Close False
  34.       End If
  35.   Next
  36.   
  37.   N = fd.SubFolders.Count
  38.   If N > 0 Then
  39.     For Each sfd In fd.SubFolders
  40.       Outputfiles sfd.Path
  41.     Next
  42.   End If

  43. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-28 09:10 | 显示全部楼层
If Len(.Sheets("开始").Name) = 0 Then,运行到这一步下标又越界了,但是我文件夹里边是有这个名称的工作表的
回复

使用道具 举报

 楼主| 发表于 2013-6-28 11:30 | 显示全部楼层
hwc2ycy 发表于 2013-6-28 09:03
你的代码不能实现遍历子文件夹。

那个代码实现不了结果啊
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 07:58 , Processed in 0.210888 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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