Excel 发送函数结果以在子系统中使用

Excel 发送函数结果以在子系统中使用,excel,function,recursion,outlook,vba,Excel,Function,Recursion,Outlook,Vba,我有一个sub,它调用一个函数,该函数搜索子文件夹,然后返回一个带有文件名的文件路径,以便主sub可以使用该字符串创建附件。它是一个递归函数,因此它会不断重置strJDFile中的值。我需要它按原样搜索所有子文件夹,找到我的文件,然后将strJDFile值发送到主sub。因为它一直在重置,没有任何东西可以发送到sub。我做错了什么?该函数在其他情况下工作。这只是实现结果的最后一步 Function recurse(sPath As String) Dim FSO As New FileSyste

我有一个sub,它调用一个函数,该函数搜索子文件夹,然后返回一个带有文件名的文件路径,以便主sub可以使用该字符串创建附件。它是一个递归函数,因此它会不断重置strJDFile中的值。我需要它按原样搜索所有子文件夹,找到我的文件,然后将strJDFile值发送到主sub。因为它一直在重置,没有任何东西可以发送到sub。我做错了什么?该函数在其他情况下工作。这只是实现结果的最后一步

Function recurse(sPath As String)
Dim FSO As New FileSystemObject
Dim myFolder As Scripting.Folder
Dim mySubFolder As Scripting.Folder
Dim myFile As Scripting.File
Dim strName As String
Dim strJDFile As String
Dim strDir As String
Dim strJDName As String


Set myFolder = FSO.GetFolder(sPath)
strName = Range("a2").Offset(0, 3)

For Each mySubFolder In myFolder.SubFolders
    For Each myFile In mySubFolder.Files
        If myFile.name Like "*" & strName & "*" Then
            strJDName = myFile.name 
            strDir = mySubFolder & "\"
            strJDFile = strDir & strJDName
            Exit Function
        End If
    Next
    recurse = recurse(mySubFolder.Path)
Next

End Function
我查看了关于这个问题的多篇文章,包括这篇文章,我在那里对答案投了赞成票,但这是如何设置递归,而不是如何将值传递给子对象。问题是,正如我上面所说的,每次它到达“下一个”时都会重置,因此我的strJDFile值再次设置为“”。但是您需要递归strDir之后的下一个,以便让它继续进入下一个子文件夹,直到if找到正确的值。我只需要保留该值,而不是将其作为空白。我用F8进行了一步,这就是我发现当它进入下一个决赛时,它会重置的原因。这就是为什么我添加了Exit函数,但它也不起作用。

返回的是“recurse”,而不是strJDFile

Private Sub functionTest()

Dim x As String
Dim fPath As String

fPath = "C:\Test"

x = recurse(fPath)

If x = "" Then x = "No results."

Debug.Print " *** recurse has returned: " & x
Debug.Print "Done"

End Sub


Function recurse(sPath As String)

Dim FSO As New FileSystemObject
Dim myFolder As Scripting.folder
Dim mySubFolder As Scripting.folder
Dim myFile As Scripting.file

Dim strName As String
Dim strJDFile As String
Dim strDir As String
Dim strJDName As String

Set myFolder = FSO.GetFolder(sPath)

' strName = Range("a2").Offset(0, 3)
strName = "test.xlsx"

For Each mySubFolder In myFolder.SubFolders

    Debug.Print " mySubFolder: " & mySubFolder

    For Each myFile In mySubFolder.Files

        If myFile.name Like "*" & strName & "*" Then
            strJDName = myFile.name
            strDir = mySubFolder & "\"
            strJDFile = strDir & strJDName

            recurse = strJDFile

            Exit Function

        Else
            Debug.Print "  myFile.name: " & myFile.name

        End If

    Next

    recurse = recurse(mySubFolder.path)

Next

End Function
返回“recurse”,而不是strJDFile

Private Sub functionTest()

Dim x As String
Dim fPath As String

fPath = "C:\Test"

x = recurse(fPath)

If x = "" Then x = "No results."

Debug.Print " *** recurse has returned: " & x
Debug.Print "Done"

End Sub


Function recurse(sPath As String)

Dim FSO As New FileSystemObject
Dim myFolder As Scripting.folder
Dim mySubFolder As Scripting.folder
Dim myFile As Scripting.file

Dim strName As String
Dim strJDFile As String
Dim strDir As String
Dim strJDName As String

Set myFolder = FSO.GetFolder(sPath)

' strName = Range("a2").Offset(0, 3)
strName = "test.xlsx"

For Each mySubFolder In myFolder.SubFolders

    Debug.Print " mySubFolder: " & mySubFolder

    For Each myFile In mySubFolder.Files

        If myFile.name Like "*" & strName & "*" Then
            strJDName = myFile.name
            strDir = mySubFolder & "\"
            strJDFile = strDir & strJDName

            recurse = strJDFile

            Exit Function

        Else
            Debug.Print "  myFile.name: " & myFile.name

        End If

    Next

    recurse = recurse(mySubFolder.path)

Next

End Function

可能重复的
recurse=strJDFile
或直接
recurse=strDir&strJDName
@ashleedawg,谢谢,我在上面添加了一个编辑来进一步解释我的问题。这篇文章对我的问题非常重要,但仍然不能帮助我解决函数结束前重置值的问题。@niton,非常感谢,我也试过了。我需要strJDFile的完整值才能用于我的附件,但在退出函数之前,它仍然重置为“”。每次点击下一个按钮时,它都会重置。我以为退出功能可以解决这个问题,但没有乐趣。我确实点击了你的链接,我也尝试过使用Set关键字,但它出错了,说strJDFile需要一个对象(编译错误:Object Required)@niton,我一直在尝试其他选项,包括添加一个if stmt,如果strJDFile不是空的,那么退出函数(或结束函数),但它仍然将strJDFile重置为空,然后就没有结果通过了完整的子,这意味着没有附件。我不确定我做错了什么,递归函数真的可以返回要使用的值吗?感谢可能重复的
recurse=strJDFile
或直接
recurse=strDir&strJDName
@ashleedawg,谢谢,我在上面添加了一个编辑来进一步解释我的问题。这篇文章对我的问题非常重要,但仍然不能帮助我解决函数结束前重置值的问题。@niton,非常感谢,我也试过了。我需要strJDFile的完整值才能用于我的附件,但在退出函数之前,它仍然重置为“”。每次点击下一个按钮时,它都会重置。我以为退出功能可以解决这个问题,但没有乐趣。我确实点击了你的链接,我也尝试过使用Set关键字,但它出错了,说strJDFile需要一个对象(编译错误:Object Required)@niton,我一直在尝试其他选项,包括添加一个if stmt,如果strJDFile不是空的,那么退出函数(或结束函数),但它仍然将strJDFile重置为空,然后就没有结果通过了完整的子,这意味着没有附件。我不确定我做错了什么,递归函数真的可以返回要使用的值吗?谢谢你,你是最棒的!!呜呜,非常感谢你!我在stackoverflow周围看到了你的答案,你帮助了这么多人,再次感谢你!在我将目录更改为我自己的目录,然后将main子目录中的strJDFile指向recurse函数返回的值之后,这一点非常有效。我希望这段代码能帮助其他人。再次感谢你!呜呜!顺便说一句,我有一个错误,那就是在退出函数之前没有递归=strJDFile行……debug.print行是否也有区别,或者这只是为了让我能在即时框中看到它?我只是想知道,以供将来参考。调试行对结果没有影响。很好,谢谢…让我惊讶的是,行的位置有如此大的影响。尼顿,你是最好的!!呜呜,非常感谢你!我在stackoverflow周围看到了你的答案,你帮助了这么多人,再次感谢你!在我将目录更改为我自己的目录,然后将main子目录中的strJDFile指向recurse函数返回的值之后,这一点非常有效。我希望这段代码能帮助其他人。再次感谢你!呜呜!顺便说一句,我有一个错误,那就是在退出函数之前没有递归=strJDFile行……debug.print行是否也有区别,或者这只是为了让我能在即时框中看到它?我只是想知道,以供将来参考。调试行对结果没有影响。很好,谢谢…让我惊讶的是,行的位置有这样的影响。