我的 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