2013-10-18 62 views
-1

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

你明白你的代码?你可以让你的'do ... loop'在每次调用其他Sub时结束1.000.000次。如果我们没有看到你的工作表,工作簿等,就很难帮助你。我唯一的想法 - 试着用'F8'来运行它,这是一种调试选项...... –

+0

在递归中对于每个objFile在objFolder.Files'中,你确定你没有取回文件“。”和“..”...如果你这样做了,你必须将它们排除在发现之外,因为它们指向自己......在F5的子程序getNewestFile()(F9)处设置一个断点并检查对象objFile '在每个循环之后使用本地窗口。 – MikeD

+0

您是否尝试在调试模式下逐步执行代码?做到这一点,并让我们知道你发现了什么。 –

回答

0

我已经做了一些改动你的代码。这会减慢你的进程,但它不应该崩溃。我测试了5行数据,例如5 main folders6883子文件夹,46413文件),它运行得很好。

一旦测试结束后,删除其在他们subfoldercountfilescount线

Option Explicit 

Public newestFile As Object 
Dim subfoldercount As Long, filescount As Long 

Sub Scan_Click() 
    Dim path As String 
    Dim row As Integer: row = 2 
    Dim ws As Worksheet 

    Set ws = ThisWorkbook.Sheets("ETA File Server") 

    subfoldercount = 0: filescount = 0 

    With ws 
     Do 
      If .Cells(row, 1).Value = "" Then Exit Do 

      path = .Cells(row, 1).Value 

      Application.StatusBar = "Processing folder " & path 
      DoEvents 

      If Not .Cells(row, 1).Value = "Root" Then 
       Call getNewestFile(path) 

       .Cells(row, 7).Value = subfoldercount 
       .Cells(row, 8).Value = filescount 
       .Cells(row, 9).Value = newestFile.DateLastModified 
       .Cells(row, 10).Value = newestFile.Name 

       Set newestFile = Nothing 
       subfoldercount = 0: filescount = 0 
       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 
     subfoldercount = subfoldercount + 1 
     Call getNewestFile(objFile.path) 
     DoEvents 
    Next 


    For Each objFile In objFolder.Files 
     filescount = filescount + 1 
     If newestFile Is Nothing Then 
      Set newestFile = objFile 
     ElseIf objFile.DateLastModified > newestFile.DateLastModified Then 
      Set newestFile = objFile 
     End If 
    Next 
End Sub 

enter image description here

+0

我确实改变了它,它仍然不工作,我认为这是因为数据量。我改变了代码,如果不是'代码',因为它没有工作,并且我还要添加一行= row + 1,因为它没有继续发展... 在前两行停止写入后细胞,但脚本仍在运行......这可能表明Excel过度训练。 – Chris

+0

另一个问题出现了,似乎路径对于一个字符串来说太长了,是否应该在更多的部分打破路径或者是否有更好的解决方案? – Chris