陷入循环。使用 Excel VBA 的目录列表

Stuck in a Loop. Directory Listing using Excel VBA

我正在尝试在此处列出我的驱动器的目录列表,其中包含目录 subdirectories/folders 和文件。它还显示文件名、文件路径、日期和大小。 我目前正在使用此代码:

Dim iRow

Sub ListFiles()
    iRow = 11 'starting row listing
    Call ListMyFiles(Range("B5"), Range("B6"))
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Set MyObject = New Scripting.FileSystemObject
    Set mySource = MyObject.GetFolder(mySourcePath)
        On Error Resume Next
        For Each myFile In mySource.Files
        iCol = 3
        Cells(iRow, iCol).Value = myFile.Path
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Name
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Size
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.DateLastModified
        iRow = iRow + 1
    Next
    If IncludeSubfolders Then
        For Each mySubFolder In mySource.SubFolders
            Call ListMyFiles(mySource.Path, True)
        Next
    End If
End Sub

每当我输入一个地址时,它都能正常工作,但是当我决定包含子文件夹时,它就会变得混乱!它只是永远循环下去。它只是不停。 我不知道在哪里寻找错误。我一直在重读它,它看起来还不错,但也许这就是我。另外,为了记录,我勾选了 Microsoft Scripting Runtime。 而且,我想我还缺少一些东西,因为我也想显示文件夹名称,如果可能的话。

这个问题可能不清楚或者可能已经重复了,所以请告诉我。谢谢!

P.S。 有点像这里的新手。

传递给子文件夹列表函数的路径值是错误的。

应该是:

Call ListMyFiles(mySubFolder.Path, True)