Return VBA中自调用函数中的数组

Return Array in Self-Calling Function in VBA

我正在尝试弄清楚如何 return 一个调用自身的函数中的字符串数组。该功能可查看特定路径内的所有文件夹和子文件夹,以生成文本文件列表及其所在位置。现在,子例程是 All_Folder。我以前曾尝试使 All_Folder 成为一个函数,并尝试 return 函数中的值,但这让我无处可去。这是我的代码:

Public file_locations() As String
Sub Some_Subroutine()
    Dim pathway As String: pathway = "C:\Users"
    Dim FileSystem As Object
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    All_Folder FileSystem.GetFolder(pathway)

    ...

End Sub
Public Sub All_Folder(Folder)
    Dim filetype as string: filetype = "*.txt"
    Dim Counter As Integer: Counter = 0
    Dim SubFolder: Dim File
    For Each SubFolder in Folder.SubFolders
        All_Folder SubFolder
    Next
    For Each File in Folder.Files
        If Dir(Folder & "\" & filetype) <> "" Then
            ReDim Preserve file_locations(Counter)
            file_locations(Counter) = CallByName(File, Path, VbGet)
            Counter = Counter + 1
        End If
    Next
End Sub

我的问题是每次此函数 运行 时,file_locations 中的输出总是被下一个调用目录中的内容覆盖。

我在子例程 All_Folder 的末尾有列表,我只需要能够 store/append 它到当前的 file_locations 列表。

正如 Tim Williams and PeH 所建议的,我没有将我的 Counter 设置为全局变量。这是更新后的代码:

Public file_locations() As String
Public Counter As Integer
Sub Some_Subroutine()
    Dim pathway As String: pathway = "C:\Users"
    Counter = 0
    Dim FileSystem As Object
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    All_Folder FileSystem.GetFolder(pathway)

    ...

End Sub
Public Sub All_Folder(Folder)
    Dim filetype as string: filetype = "*.txt"
    Dim SubFolder: Dim File
    For Each SubFolder in Folder.SubFolders
        All_Folder SubFolder
    Next
    For Each File in Folder.Files
        If Dir(Folder & "\" & filetype) <> "" Then
            ReDim Preserve file_locations(Counter)
            file_locations(Counter) = CallByName(File, Path, VbGet)
            Counter = Counter + 1
        End If
    Next
End Sub

感谢两位帮助我。