我的VBA程序每次运行时都停止工作。我找不到这个错误。 没有错误信息; Excel只停止工作。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'在每次调用其他Sub时结束1.000.000次。如果我们没有看到你的工作表,工作簿等,就很难帮助你。我唯一的想法 - 试着用'F8'来运行它,这是一种调试选项...... –
在递归中对于每个objFile在objFolder.Files'中,你确定你没有取回文件“。”和“..”...如果你这样做了,你必须将它们排除在发现之外,因为它们指向自己......在F5的子程序getNewestFile()(F9)处设置一个断点并检查对象objFile '在每个循环之后使用本地窗口。 – MikeD
您是否尝试在调试模式下逐步执行代码?做到这一点,并让我们知道你发现了什么。 –