我的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
您是否嘗試在調試模式下逐步執行代碼?做到這一點,並讓我們知道你發現了什麼。 –