Excel精英培训网

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

[已解决]遍历间隔

[复制链接]
发表于 2016-6-24 11:42 | 显示全部楼层 |阅读模式
本帖最后由 乐乐2006201506 于 2016-6-24 11:45 编辑

        怎样使下面代码在打开一个工作薄写入代码后,间隔一定时间另存,关闭,然后再打开第二个工作簿,间隔一定时间保存,关闭……以此类推。谢谢!
附件在([已解决]实现另存为代码正常运行,http://www.excelpx.com/thread-419221-1-1.html
Dim ArrFiles(1 To 10000)
Dim cntFiles%
Public Sub ListAllFiles()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim strPath$
Dim i%
Dim fso As New FileSystemObject, fd As Folder
strPath = "C:\Users\Administrator\Desktop\写入代码多层子文件夹\花名册测试文件\"
cntFiles = 0
Set fd = fso.GetFolder(strPath)
SearchFiles fd
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

  • Sub SearchFiles(ByVal fd As Folder)
  • Dim fl As File
  • Dim sfd As Folder
  • For Each fl In fd.Files                                        '通过循环把文件逐个放在数组内
  • cntFiles = cntFiles + 1
  • ArrFiles(cntFiles) = fl.Path
  •     If fl Like "*.xls" Then
  •         Workbooks.Open fl
  •         With ActiveWorkbook.VBProject.VBComponents("thisworkbook").CodeModule
  •             .InsertLines 1, "sub test()"
  •             .InsertLines 2, "msgbox ""just a test"""            '双引号中的双引号,2个代表1个.
  •             .InsertLines 3, "end sub"
  •         End With
  •         ActiveWorkbook.SaveAs Filename:=Replace(fl, ".xls", ".xlsm"), FileFormat:=52
  •         ActiveWorkbook.Close True
  •     End If
  • Next fl
  • If fd.SubFolders.Count = 0 Then Exit Sub                    'SubFolders返回由指定文件夹中所有子文件夹(包括隐藏文件夹和系统文件夹)组成的 Folders 集合
  • For Each sfd In fd.SubFolders                               '在 Folders 集合进行循环查找
  • SearchFiles sfd                                             '使用递归方法查找下一个文件夹
  • Next
  • End Sub

最佳答案
2016-6-24 12:12
加个延迟API函数最简单:
  1. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  2. Dim ArrFiles(1 To 10000) '创建一个数组空间,用来存放文件名称
  3. Dim cntFiles% '文件个数
  4. Public Sub ListAllFiles()
  5.     Application.ScreenUpdating = False
  6.     Application.DisplayAlerts = False

  7. Dim strPath$                                                        '声明文件路径
  8. Dim i%
  9. 'Set fso = CreateObject("Scripting.FileSystemObject")
  10. Dim fso As New FileSystemObject, fd As Folder                       '创建一个FileSystemObject对象和一个文件夹对象
  11.         strPath = "C:\Users\Administrator\Desktop\写入代码多层子文件夹\花名册测试文件" '"设置要遍历的文件夹目录
  12.         cntFiles = 0
  13.         Set fd = fso.GetFolder(strPath)                             '设置fd文件夹对象
  14.         SearchFiles fd                                              '调用子程序查搜索文件
  15. '        Sheets(1).Range("A1").Resize(cntFiles) = Application.Transpose(ArrFiles)      '把数组内的路径和文件名放在单元格中
  16.     Application.DisplayAlerts = True
  17.     Application.ScreenUpdating = True
  18. End Sub
  19. Sub SearchFiles(ByVal fd As Folder)
  20. Dim fl As File
  21. Dim sfd As Folder
  22. For Each fl In fd.Files                                        '通过循环把文件逐个放在数组内
  23. cntFiles = cntFiles + 1
  24. ArrFiles(cntFiles) = fl.Path
  25.     If fl Like "*.xls" Then
  26.         Workbooks.Open fl
  27.         With ActiveWorkbook.VBProject.VBComponents("thisworkbook").CodeModule
  28.             .InsertLines 1, "sub test()"
  29.             .InsertLines 2, "msgbox ""just a test"""            '双引号中的双引号,2个代表1个.
  30.             .InsertLines 3, "end sub"
  31.         End With
  32.         Sleep 3000   '延迟时间,毫秒
  33.         ActiveWorkbook.SaveAs Filename:=Replace(fl, ".xls", ".xlsm"), FileFormat:=52
  34.         ActiveWorkbook.Close True
  35.     End If
  36. Next fl
  37. If fd.SubFolders.Count = 0 Then Exit Sub                    'SubFolders返回由指定文件夹中所有子文件夹(包括隐藏文件夹和系统文件夹)组成的 Folders 集合
  38. For Each sfd In fd.SubFolders                               '在 Folders 集合进行循环查找
  39. SearchFiles sfd                                             '使用递归方法查找下一个文件夹
  40. Next
  41. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-6-24 12:12 | 显示全部楼层    本楼为最佳答案   
加个延迟API函数最简单:
  1. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  2. Dim ArrFiles(1 To 10000) '创建一个数组空间,用来存放文件名称
  3. Dim cntFiles% '文件个数
  4. Public Sub ListAllFiles()
  5.     Application.ScreenUpdating = False
  6.     Application.DisplayAlerts = False

  7. Dim strPath$                                                        '声明文件路径
  8. Dim i%
  9. 'Set fso = CreateObject("Scripting.FileSystemObject")
  10. Dim fso As New FileSystemObject, fd As Folder                       '创建一个FileSystemObject对象和一个文件夹对象
  11.         strPath = "C:\Users\Administrator\Desktop\写入代码多层子文件夹\花名册测试文件" '"设置要遍历的文件夹目录
  12.         cntFiles = 0
  13.         Set fd = fso.GetFolder(strPath)                             '设置fd文件夹对象
  14.         SearchFiles fd                                              '调用子程序查搜索文件
  15. '        Sheets(1).Range("A1").Resize(cntFiles) = Application.Transpose(ArrFiles)      '把数组内的路径和文件名放在单元格中
  16.     Application.DisplayAlerts = True
  17.     Application.ScreenUpdating = True
  18. End Sub
  19. Sub SearchFiles(ByVal fd As Folder)
  20. Dim fl As File
  21. Dim sfd As Folder
  22. For Each fl In fd.Files                                        '通过循环把文件逐个放在数组内
  23. cntFiles = cntFiles + 1
  24. ArrFiles(cntFiles) = fl.Path
  25.     If fl Like "*.xls" Then
  26.         Workbooks.Open fl
  27.         With ActiveWorkbook.VBProject.VBComponents("thisworkbook").CodeModule
  28.             .InsertLines 1, "sub test()"
  29.             .InsertLines 2, "msgbox ""just a test"""            '双引号中的双引号,2个代表1个.
  30.             .InsertLines 3, "end sub"
  31.         End With
  32.         Sleep 3000   '延迟时间,毫秒
  33.         ActiveWorkbook.SaveAs Filename:=Replace(fl, ".xls", ".xlsm"), FileFormat:=52
  34.         ActiveWorkbook.Close True
  35.     End If
  36. Next fl
  37. If fd.SubFolders.Count = 0 Then Exit Sub                    'SubFolders返回由指定文件夹中所有子文件夹(包括隐藏文件夹和系统文件夹)组成的 Folders 集合
  38. For Each sfd In fd.SubFolders                               '在 Folders 集合进行循环查找
  39. SearchFiles sfd                                             '使用递归方法查找下一个文件夹
  40. Next
  41. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 15:22 , Processed in 0.254407 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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