【发布时间】:2013-10-27 01:20:25
【问题描述】:
我的 VBA 程序每次运行时都会停止工作。我就是找不到错误。 没有错误信息; Excel 只是停止工作。
这是我的代码:
Option Explicit
Public newestFile As Object
Sub Scan_Click()
Dim row As Integer: row = 2
Do
If Sheets("ETA File Server").Cells(row, 1) <> "" Then
Dim path As String: path = Sheets("ETA File Server").Cells(row, 1)
If Sheets("ETA File Server").Cells(row, 1) = "Root" Then
row = row + 1
Else
Call getNewestFile(path)
Sheets("ETA File Server").Cells(row, 10) = newestFile.Name
Sheets("ETA File Server").Cells(row, 9) = newestFile.DateLastModified
row = row + 1
End If
Else
Exit Do
End If
Loop
row = 2
End Sub
Private Sub getNewestFile(folderPath As String)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
'get the filesystem object from the system
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(folderPath)
'go through the subfolder and call itself
For Each objFile In objFolder.SubFolders
Call getNewestFile(objFile.path)
Next
For Each objFile In objFolder.Files
If newestFile Is Nothing Then
Set newestFile = objFile
ElseIf objFile.DateLastModified > newestFile.DateLastModified Then
Set newestFile = objFile
End If
Next
End Sub
【问题讨论】:
-
你了解你的代码吗?你让你的
do...loop每次调用其他子时可以工作超过 1.000.000 次。如果我们看不到您的工作表、工作簿等,很难为您提供帮助。我唯一的想法 - 尝试使用F8运行它,这是一种调试选项... -
在递归
For Each objFile In objFolder.Files- 你确定你没有取回文件“。”和“..” ...如果这样做,则必须将它们从查找中排除,因为它们指向自己...在Sub getNewestFile()(F9)处设置断点,并使用F8检查对象objFile每个循环后的本地窗口。 -
您是否尝试过在调试模式下单步执行您的代码?这样做,让我们知道你发现了什么。
-
是的,我确实调试过代码,我认为问题在于数据量
标签: excel error-handling vba