Vba 搜索工作簿中的字符串-运行时错误424(需要对象)

Vba 搜索工作簿中的字符串-运行时错误424(需要对象),vba,excel,search,Vba,Excel,Search,我试图使用宏在Excel工作簿中搜索特定字符串。我想获取在其中找到字符串的单元格的地址,然后将它们一个接一个地放在当前工作表的I列中。我的代码和问题如下 Option Explicit Sub Find_Data() Dim datatoFind As String Dim rangeSearch As Range Dim rangeLast As Range Dim foundRange As Range Dim strFirstAddress As String Dim sheetCou

我试图使用宏在Excel工作簿中搜索特定字符串。我想获取在其中找到字符串的单元格的地址,然后将它们一个接一个地放在当前工作表的
I
列中。我的代码和问题如下

Option Explicit

Sub Find_Data()

Dim datatoFind As String
Dim rangeSearch As Range
Dim rangeLast As Range
Dim foundRange As Range
Dim strFirstAddress As String
Dim sheetCount As Integer
Dim sheetCounter As Integer
Dim currentSheet As Integer
Dim foundmatrixCounter As Integer
foundmatrixCounter = 2 'initialize this to the second row so the total can be placed in the first row when done

'set search range
Set rangeSearch = ActiveSheet.Range("B2:X100")

'set last cell in range
Set rangeLast = rangeSearch.Cells(rangeSearch.Cells.Count)

currentSheet = ActiveSheet.Index
datatoFind = InputBox("Please enter the value to search for")
If datatoFind = "" Then Exit Sub
sheetCount = ActiveWorkbook.Sheets.Count

For sheetCounter = 1 To sheetCount
    Sheets(sheetCounter).Activate
    Set foundRange = Cells.Find(What:=datatoFind, After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
    'if datatoFind is found in search range
    If Not foundRange Is Nothing Then
        'save the address of the first occurrence of datatoFind, in the strFirstAddress variable
        strFirstAddress = foundRange.Address
        Do
            'Find next occurrence of datatoFind
            Set foundRange = foundRange.FindNext(foundRange)
            'Place the address of this occurrence in the next cell down in the column that holds found values (i column)
            Cells(foundmatrixCounter, 9).Value = foundRange.Address
            'Increment the loop counter for the i column
            foundmatrixCounter = foundmatrixCounter + 1
            'The Loop ends on reaching the first occurrence of datatoFind
        Loop Until foundRange.Address = strFirstAddress
    End If
    Cells(1, 9).Value = foundmatrixCounter 'Put the total number of instances, in this case foundmatrixCounter, in Z1
Next sheetCounter

If foundRange Is Nothing Then
MsgBox ("Value not found")
Sheets(currentSheet).Activate
End If
End Sub
我发现了错误

运行时错误424(需要对象)

在以下行:

Set foundRange = Cells.Find(What:=datatoFind, After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate

不确定该行的内容或代码是否有误。

删除该行末尾的。激活

没有必要激活任何东西。 但是,这种格式也是不正确的。这将类似于:

Dim R as Range
set R = Range("A1").Activate

由于Range(“A1”).Activate不是一个对象(它是一个布尔值),这将导致相同的错误

Ron是正确的。设置范围对象,然后通过对范围对象进行测试来检查是否找到了datatoFind。然后你可以激活范围

Set foundRange=Cells.Find(What:=datatoFind,After:=Cells(1,1),LookIn:=xlFormulas,LookAt:=xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)

如果未找到任何内容,则foundRange.Activate

删除
.Activate
,因为当它找不到任何内容时,它无法激活单元格,从而导致error@tigeravatar事实上,无论它是否发现了什么,错误都会发生。@RonRosenfeld真的吗?真奇怪。当我在没有
.Activate
的情况下测试它时,它会为我成功运行。当然,当我使用
.Activate
运行它时,我会得到一个“objectvariableontset”错误,而不是OP得到的错误/shrug@tigeravatar也许我不清楚或者误解了你写的东西。无论是否找到内容,“.Activate”存在,都会发生错误。没有.Activate,它运行正常。为什么OP要“激活”范围?@RonRosenfeld说实话,我没有阅读所有的代码——只是导致错误的摘录。阅读完整代码后,没有理由激活范围。我不这么认为,但我偶尔会错过一些东西,因此我提出了问题。