VBA退出功能不工作?函数第一次调用成功,后续调用失败
我有两个功能。第一个循环遍历excel中的一个区域,第二个循环根据您提供的搜索字符串查找文件,并将其传递回第一个循环 第二个函数的工作方式是,在获得搜索字符串的第一个匹配项时退出该函数。但是,它仅在通过函数的第一次运行时退出,而不用于任何后续运行。相反,它跳到下一个i 我已经使用F8,我可以看到它点击了退出功能,但不管怎样它都会继续运行该功能 我做错了什么 职能1:VBA退出功能不工作?函数第一次调用成功,后续调用失败,vba,excel,Vba,Excel,我有两个功能。第一个循环遍历excel中的一个区域,第二个循环根据您提供的搜索字符串查找文件,并将其传递回第一个循环 第二个函数的工作方式是,在获得搜索字符串的第一个匹配项时退出该函数。但是,它仅在通过函数的第一次运行时退出,而不用于任何后续运行。相反,它跳到下一个i 我已经使用F8,我可以看到它点击了退出功能,但不管怎样它都会继续运行该功能 我做错了什么 职能1: Function GrabQuestionnaireLocations() Dim a As Range, b As R
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
函数FindFilePath(ByRef FolderPath作为字符串,ByVal v作为字符串)作为字符串
Dim FileName为字符串,fullFilePath为字符串,numFolders为长,Folders()为字符串,i为长
Dim objFSO作为对象,f作为对象
如果正确(FolderPath,1)“\”则FolderPath=FolderPath&“\”
FileName=Dir(FolderPath&“***”,vbDirectory)
而Len(文件名)0
如果左(文件名,1)”,则
fullFilePath=FolderPath&FileName
如果(GetAttr(fullFilePath)和vbDirectory)=vbDirectory,则
重拨将文件夹(0到numFolders)保留为字符串
文件夹(numFolders)=完整文件路径
numFolders=numFolders+1
其他的
如果InStr(fullFilePath,v)>0,则
FindFilePath=fullFilePath
退出功能
如果结束
如果结束
如果结束
FileName=Dir()
温德
对于i=0到numFolders-1
FindFilePath文件夹(i),v
接下来我
端函数
这是因为您在递归调用函数-从函数本身内部调用它(在i=0到numFolders-1的循环中)
如果您在它从For循环中调用自身时告诉它退出,它将返回到For循环中的执行
在此处声明一个布尔变量并将其设置为True,而不是在此位置使用退出函数。使用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
函数FindFilePath(ByRef FolderPath作为字符串,ByVal v作为字符串)作为字符串
未找到布尔值
Dim FileName为字符串,fullFilePath为字符串,numFolders为长,Folders()为字符串,i为长
Dim objFSO作为对象,f作为对象
如果正确(FolderPath,1)“\”则FolderPath=FolderPath&“\”
FileName=Dir(FolderPath&“***”,vbDirectory)
执行While Len(文件名)0
如果左(文件名,1)”,则
fullFilePath=FolderPath&FileName
如果(GetAttr(fullFilePath)和vbDirectory)=vbDirectory,则
重拨将文件夹(0到numFolders)保留为字符串
文件夹(numFolders)=完整文件路径
numFolders=numFolders+1
其他的
如果InStr(fullFilePath,v)>0,则
FindFilePath=fullFilePath
bMatchFound=True
退出Do
如果结束
如果结束
如果结束
FileName=Dir()
环
对于i=0到numFolders-1
如果找到bmatchfind,则退出
FindFilePath文件夹(i),v
接下来我
端函数