【问题标题】:Excel stops working, can't find the mistake [closed]Excel停止工作,找不到错误[关闭]
【发布时间】:2013-10-27 01:20:25
【问题描述】:

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

【问题讨论】:

  • 你了解你的代码吗?你让你的do...loop 每次调用其他子时可以工作超过 1.000.000 次。如果我们看不到您的工作表、工作簿等,很难为您提供帮助。我唯一的想法 - 尝试使用 F8 运行它,这是一种调试选项...
  • 在递归For Each objFile In objFolder.Files- 你确定你没有取回文件“。”和“..” ...如果这样做,则必须将它们从查找中排除,因为它们指向自己...在Sub getNewestFile()(F9)处设置断点,并使用F8检查对象objFile每个循环后的本地窗口。
  • 您是否尝试过在调试模式下单步执行您的代码?这样做,让我们知道你发现了什么。
  • 是的,我确实调试过代码,我认为问题在于数据量

标签: excel error-handling vba


【解决方案1】:

我对您的代码进行了一些更改。这会稍微减慢你的进程,但它不应该崩溃。我测试了 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

【讨论】:

  • 我确实改了,还是不行,我想是因为数据量太大。我更改了codeIf not code,因为它不起作用,而且我还想添加一行 = 行 + 1,因为它没有继续下去......在前两行之后它停止在单元格中写入,但脚本仍在运行...这可能是 Excel 过度紧张的迹象。
  • 出现另一个问题,似乎路径对于一个字符串来说太长了,是否应该在更多部分中断路径还是有更好的解决方案?
猜你喜欢
  • 1970-01-01
  • 2021-05-20
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多