2013-10-18 81 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