VBA中自调用函数的返回数组

VBA中自调用函数的返回数组,vba,powerpoint,Vba,Powerpoint,我试图弄清楚如何在调用自身的函数中返回字符串数组。它是一个函数,用于查看特定路径中的所有文件夹和子文件夹,以生成文本文件及其所在位置的列表。现在,子例程是All\u Folder。我以前尝试过将All_Folder设置为一个函数,并尝试从该函数返回值,但我没有得到任何结果。这是我现在的代码: Public file_locations() As String Sub Some_Subroutine() Dim pathway As String: pathway = "C:\Users"

我试图弄清楚如何在调用自身的函数中返回字符串数组。它是一个函数,用于查看特定路径中的所有文件夹和子文件夹,以生成文本文件及其所在位置的列表。现在,子例程是
All\u Folder
。我以前尝试过将
All_Folder
设置为一个函数,并尝试从该函数返回值,但我没有得到任何结果。这是我现在的代码:

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
Public file_locations()作为字符串
子例程()
将路径设置为字符串:pathway=“C:\Users”
将文件系统作为对象
设置FileSystem=CreateObject(“Scripting.FileSystemObject”)
所有文件夹FileSystem.GetFolder(路径)
...
端接头
公用子全部文件夹(文件夹)
将文件类型设置为字符串:filetype=“*.txt”
作为整数的Dim计数器:计数器=0
Dim子文件夹:Dim文件
对于Folder.SubFolders中的每个子文件夹
所有文件夹子文件夹
下一个
对于文件夹.Files中的每个文件
如果Dir(文件夹&“\”&filetype)”,则
重拨保留文件位置(计数器)
文件位置(计数器)=CallByName(文件、路径、VbGet)
计数器=计数器+1
如果结束
下一个
端接头
我的问题是,每次运行此函数时,
文件位置
中的输出总是被下一个目录中的内容覆盖

我在子例程
All\u文件夹的末尾有一个列表
,我只需要能够将它存储/附加到当前的
文件位置
列表。

正如所建议的,我没有将我的
计数器
设置为全局变量。以下是更新后的代码:

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
Public file_locations()作为字符串
作为整数的公共计数器
子例程()
将路径设置为字符串:pathway=“C:\Users”
计数器=0
将文件系统作为对象
设置FileSystem=CreateObject(“Scripting.FileSystemObject”)
所有文件夹FileSystem.GetFolder(路径)
...
端接头
公用子全部文件夹(文件夹)
将文件类型设置为字符串:filetype=“*.txt”
Dim子文件夹:Dim文件
对于Folder.SubFolders中的每个子文件夹
所有文件夹子文件夹
下一个
对于文件夹.Files中的每个文件
如果Dir(文件夹&“\”&filetype)”,则
重拨保留文件位置(计数器)
文件位置(计数器)=CallByName(文件、路径、VbGet)
计数器=计数器+1
如果结束
下一个
端接头
感谢你们两位的帮助。

正如我所建议的,我没有将我的
计数器设置为全局变量。以下是更新后的代码:

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
Public file_locations()作为字符串
作为整数的公共计数器
子例程()
将路径设置为字符串:pathway=“C:\Users”
计数器=0
将文件系统作为对象
设置FileSystem=CreateObject(“Scripting.FileSystemObject”)
所有文件夹FileSystem.GetFolder(路径)
...
端接头
公用子全部文件夹(文件夹)
将文件类型设置为字符串:filetype=“*.txt”
Dim子文件夹:Dim文件
对于Folder.SubFolders中的每个子文件夹
所有文件夹子文件夹
下一个
对于文件夹.Files中的每个文件
如果Dir(文件夹&“\”&filetype)”,则
重拨保留文件位置(计数器)
文件位置(计数器)=CallByName(文件、路径、VbGet)
计数器=计数器+1
如果结束
下一个
端接头

感谢你们两位的帮助。

我看不到任何功能,每次点击
All\u文件夹时,sub不会返回任何提示。
您将
计数器重置为零,因此,每次写入相同的数组位置。如果要多次运行
All_文件夹
,请将
计数器
也设为全局变量:
公共计数器,只要长
,并将
Dim Counter作为整数删除:Counter=0
您检查文件类型的方法也有点错误-所有这些都是错误的检查文件夹中是否至少有一个txt文件正在处理。谢谢@TimWilliams和@PeH。我所需要的只是将我的
计数器设置为全局变量。我看不到任何函数,每次点击
All\u文件夹
时,sub不会返回任何提示,因此,每次写入相同的数组位置。如果要多次运行
All_文件夹
,请将
计数器
也设为全局变量:
公共计数器,只要长
,并将
Dim Counter作为整数删除:Counter=0
您检查文件类型的方法也有点错误-所有这些都是错误的检查文件夹中是否至少有一个txt文件正在处理。谢谢@TimWilliams和@PeH。我只需要将我的
计数器设置为全局变量。