VBA退出功能不起作用?第一次调用功能成功,随后调用失败

时间:2018-08-22 08:54:42

标签: excel vba excel-vba

我有两个职能。第一个循环浏览excel中的一个范围,第二个循环根据您提供的搜索字符串查找文件,并将其传递回第一个。

第二个函数通过在搜索字符串上第一个匹配项时退出该函数来工作。但是,它仅在该函数的第一次运行中退出,而在随后的任何运行中均不存在。相反,它跳到Next i

我已逐步使用F8,可以看到它击中Exit Function,但无论如何它都继续运行该功能。

我做错了什么?

功能1:

Function GrabQuestionnaireLocations()

    Dim a As Range, b As Range, N As Long, qPath As String

    N = Cells(Rows.Count, "A").End(xlUp).Row

    Set a = Range("A2:A" & N)

    For Each b In a.Rows

        qPath = FindFilePath(_ROOT_PATH_, b.Value)

        If Len(qPath) > 0 Then
            Cells(b.Row, "C").Value = qPath
        Else
            Cells(b.Row, "C").Value = "Questionnaire not found"
        End If

        'MsgBox qPath

    Next


End Function

功能2:

Function FindFilePath(ByRef FolderPath As String, ByVal v As String) As String

    Dim FileName As String, fullFilePath As String, numFolders As Long, Folders() As String, i As Long
    Dim objFSO As Object, f As Object

    If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
    FileName = Dir(FolderPath & "*.*", vbDirectory)

    While Len(FileName) <> 0

        If Left(FileName, 1) <> "." Then

            fullFilePath = FolderPath & FileName

            If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then

                ReDim Preserve Folders(0 To numFolders) As String
                Folders(numFolders) = fullFilePath
                numFolders = numFolders + 1

            Else

                If InStr(fullFilePath, v) > 0 Then                           

                    FindFilePath = fullFilePath                                 
                    Exit Function                                               

                End If       

            End If      

        End If              

        FileName = Dir()

    Wend                        

    For i = 0 To numFolders - 1
        FindFilePath Folders(i), v
    Next i

End Function

1 个答案:

答案 0 :(得分:1)

这是因为您要递归调用函数-从自身内部(在For i = 0 to numFolders - 1循环中)调用它。

如果您告诉它在For循环中调用自身时退出,它将返回到For循环中执行。

在此位置声明一个布尔变量并将其设置为True,而不是在该位置使用Exit Function。使用Do While...Loop代替过时的While...Wend构造,然后可以使用Exit Do退出该循环。

最后,在For循环中检查布尔值是否为True,然后退出该循环。因为那是在函数的末尾,所以函数将正常结束。更像是:

Function FindFilePath(ByRef FolderPath As String, ByVal v As String) As String
    Dim bMatchFound as Boolean
    Dim FileName As String, fullFilePath As String, numFolders As Long, Folders() As String, i As Long
    Dim objFSO As Object, f As Object

    If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
    FileName = Dir(FolderPath & "*.*", vbDirectory)

    Do While Len(FileName) <> 0    
        If Left(FileName, 1) <> "." Then   
            fullFilePath = FolderPath & FileName   
            If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then    
                ReDim Preserve Folders(0 To numFolders) As String
                Folders(numFolders) = fullFilePath
                numFolders = numFolders + 1    
            Else  
                If InStr(fullFilePath, v) > 0 Then                               
                    FindFilePath = fullFilePath                                 
                    bMatchFound = True
                    Exit Do
                End If       
            End If          
        End If              

        FileName = Dir()    
    Loop                        

    For i = 0 To numFolders - 1
        If bMatchFound Then Exit For
        FindFilePath Folders(i), v
    Next i
End Function