【发布时间】:2013-10-22 07:27:22
【问题描述】:
我收到此错误消息,我完全迷路了......
我想我检查了所有可能出错的地方,也许你们中的一个人可以看出错误或其他什么。我的大脑现在完全被阻塞了。
提前致谢
Option Explicit
Public newestFile As Object
Sub Scan_Click()
Dim path As String
Dim row As Integer: row = 2
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("ETA File Server")
With ws
Do
If .Cells(row, 1).Value = "" Then Exit Do
path = .Cells(row, 1).Value
Application.StatusBar = "Processing folder " & path
DoEvents
If .Cells(row, 1).Value <> "Root" Then
Call getNewestFile(path)
.Cells(row, 9).Value = newestFile.DateLastModified
.Cells(row, 10).Value = newestFile.Name
Set newestFile = Nothing
row = row + 1
Else
row = row + 1
End If
Loop
End With
Application.StatusBar = "Done"
End Sub
Private Sub getNewestFile(folderpath As String)
Dim objFSO As Object, objFolder As Object, 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)
DoEvents
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
【问题讨论】:
-
哪一行给你那个错误
-
For Each objFile In objFolder.SubFolders -
在调用
getNewestFile之前执行Debug.print folderPath并确保路径正确。 -
路径实际上是正确的,但路径是 251char 长,如果这可能是一个问题,我不认为但是......是的
-
不,路径可以超过 251 个字符。但是如果没有找到路径,那 99.9% 的时间意味着它不是一个有效的路径:P
标签: excel vba runtime-error