Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异常-vba错误:未设置对象变量或带块变量_Vba_Excel - Fatal编程技术网

异常-vba错误:未设置对象变量或带块变量

异常-vba错误:未设置对象变量或带块变量,vba,excel,Vba,Excel,现在我意识到,在过去有很多关于这个错误的问题被问到并得到了回答,但是这个案例有点难懂,我不知道是什么导致了这个问题 我编写了一些代码,在excel文件中搜索关键字,然后返回在哪里找到关键字的信息。对于我输入的大多数关键字,代码都可以正常工作,但有些关键字在我运行宏时会产生错误消息。如果有人能弄明白为什么那会很棒 代码是: Sub SearchFolders() Dim fso As Object Dim fld As Object Dim strSearch As String ' Keywor

现在我意识到,在过去有很多关于这个错误的问题被问到并得到了回答,但是这个案例有点难懂,我不知道是什么导致了这个问题

我编写了一些代码,在excel文件中搜索关键字,然后返回在哪里找到关键字的信息。对于我输入的大多数关键字,代码都可以正常工作,但有些关键字在我运行宏时会产生错误消息。如果有人能弄明白为什么那会很棒

代码是:

Sub SearchFolders()
Dim fso As Object
Dim fld As Object
Dim strSearch As String ' Keyword to search for
Dim strPath As String ' Filepath of folder to search
Dim strFile As String ' current file that the loop is searching through
Dim wOut As Worksheet ' Worksheet to display results
Dim wbk As Workbook ' Workbook to be searched
Dim wks As Worksheet ' Worksheet to be searched
Dim lRow As Integer
Dim rFound As Range
Dim strFirstAddress As String

Application.ScreenUpdating = False

'Change as desired
strPath = "\\ant\dept-eu\LTN1\Techies Information\aa Eng daily log"
strSearch = InputBox("Insert Keyword to search")

Set wOut = Sheet1
lRow = 1
With wOut
    Sheet1.Cells.Clear
    .Cells(lRow, 1) = "Workbook"
    .Cells(lRow, 2) = "Worksheet"
    .Cells(lRow, 3) = "Text in Cell"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(strPath)

    strFile = Dir(strPath & "\*.xls*")
    Do While strFile <> ""
        Set wbk = Workbooks.Open _
          (Filename:=strPath & "\" & strFile, _
          UpdateLinks:=0, _
          ReadOnly:=True, _
          AddToMRU:=False)

        For Each wks In wbk.Worksheets ' for each worksheet
            Set rFound = wks.UsedRange.Find(strSearch) ' setting variable to first result in find function
            If Not rFound Is Nothing Then ' if something is found
                strFirstAddress = rFound.Address ' set first address to that cell's address
            End If
            Do
                If rFound Is Nothing Then ' if nothing was found
                    Exit Do ' exit loop
                Else ' if something was found then add the details to the table
                    lRow = lRow + 1
                    .Cells(lRow, 1) = wbk.Name
                    .Cells(lRow, 2) = wks.Name
                    .Cells(lRow, 3) = rFound.Value
                End If
               Set rFound = wks.Cells.FindNext(After:=rFound)  ' sets rfound vaiable to next found value
            Loop While strFirstAddress <> rFound.Address ' once the find function gets back to the first address then exit the loop

        Next ' next worksheet in file

        wbk.Close (False)
        strFile = Dir
    Loop
    .Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"

ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub
当strFirstAddress rFound.Address行时,循环中出现错误

即使没有找到任何内容,代码也会进入Do循环

尝试以下方法:

Set rFound = wks.UsedRange.Find(what:=strSearch, lookat:=xlWhole, lookin:=xlValues) 
If Not rFound Is Nothing Then 
    strFirstAddress = rFound.Address 
    Do
        lRow = lRow + 1
        .Cells(lRow, 1) = wbk.Name
        .Cells(lRow, 2) = wks.Name
        .Cells(lRow, 3) = rFound.Value
        Set rFound = wks.Cells.FindNext(After:=rFound) 
    Loop While strFirstAddress <> rFound.Address 
End If
指定要查找的附加参数是一个好主意,因为它们的值将在两次使用之间保持不变,即使通过Excel UI使用,因此如果忽略它们,您永远无法依赖将使用哪些值。

即使未找到任何值,您的代码也会进入Do循环

尝试以下方法:

Set rFound = wks.UsedRange.Find(what:=strSearch, lookat:=xlWhole, lookin:=xlValues) 
If Not rFound Is Nothing Then 
    strFirstAddress = rFound.Address 
    Do
        lRow = lRow + 1
        .Cells(lRow, 1) = wbk.Name
        .Cells(lRow, 2) = wks.Name
        .Cells(lRow, 3) = rFound.Value
        Set rFound = wks.Cells.FindNext(After:=rFound) 
    Loop While strFirstAddress <> rFound.Address 
End If

指定要查找的其他参数是一个好主意,因为它们的值将在不同的使用之间保持不变,即使是通过Excel UI使用,因此如果忽略它们,您永远无法依赖将使用哪些值。

该错误意味着您的rFound设置为Nothing,因为它找不到strSearch。您需要更改错误检查的位置。谢谢!我现在添加了一个If rFound is Nothing then exit do语句,它现在似乎工作正常。这个错误意味着您的rFound设置为Nothing,因为它找不到strearch。您需要更改错误检查的位置。谢谢!我现在添加了一个If rFound is Nothing then exit do语句,现在似乎工作正常。谢谢,我已经整理了代码,但仍然有同样的问题。例如,当搜索word controller时,代码工作正常,搜索文件夹中的每个文件并报告所有实例。但是,搜索其他作品(如传送带)会导致宏在搜索第一个工作簿后产生91错误。我将使用当前代码创建一个新问题,并指出错误发生的位置。谢谢,我已经整理了代码,但仍然存在相同的问题。例如,当搜索word controller时,代码工作正常,搜索文件夹中的每个文件并报告所有实例。但是,搜索其他作品(如传送带)会导致宏在搜索第一个工作簿后产生91错误。我将使用当前代码创建一个新问题,并指出错误发生的位置。