查找函数运行时错误 91 中的 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9199951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:33:19  来源:igfitidea点击:

VBA in find function runtime error 91

excelvbafindruntime

提问by Brian

My question is concerning the runtime error 91 in VBA for excel. I've done some searching to no avail. My code is below. I have noted the section causing the error. Why is this happening, and how can i fix it and move on?

我的问题是关于 VBA for excel 中的运行时错误 91。我已经做了一些搜索无济于事。我的代码如下。我已经注意到导致错误的部分。为什么会发生这种情况,我该如何解决并继续?

Sub RemoveFooterRows(theFile)
    Dim found As Range
    Dim aggregateRow

    ''Error is from section below
    found = isItRow = Workbooks(theFile).Worksheets(1).Columns(1).Find _
        ("Summary", Range("A1"), xlValues, xlPart, xlByRows, xlNext, False, , False)
    ''Error is from section above

    MsgBox ("val is " & found.Row)

End Sub

回答by Jesse

Sub RemoveFooterRows(theFile)

    Dim found As Range

    Set found = Workbooks(theFile).Worksheets(1).Columns(1).Find _
        ("Summary", Range("A1"), xlValues, xlPart, xlByRows, xlNext, False, , False)
    MsgBox ("val is " & found.Row)

End Sub

You need the "Set" keyword to assign the value.

您需要“设置”关键字来分配值。

Also not sure what you want " =isItRow =" to do but you should do it in two statements instead of trying to stack them like that.

也不确定您想要“=isItRow=”做什么,但您应该在两个语句中进行操作,而不是尝试像那样堆叠它们。

Also aggregateRow isn't used and isn't assigned a type.

也没有使用aggregateRow,也没有分配类型。

回答by Jerry Beaucaire

You use SET to find a cell and put it in an object. But you can let SET out if you just waht the Row. This is how I would write that test so far:

您使用 SET 查找单元格并将其放入对象中。但是,如果你只是在 Row 上,你可以让 SET 出去。到目前为止,这是我编写该测试的方式:

Dim Rw As Long

On Error Resume Next
Rw = Workbooks(theFile).Worksheets(1).Columns(1).Find _
    ("Summary", LookIn:=xlValues, LookAt:=xlPart).Row

If Rw > 0 Then
    MsgBox "The row is " & Rw
Else
    MsgBox "Not found"
End If

回答by aileen

Sub search()

    Sheets("MyShelf").Activate

    Dim dd() As String

    aa = Cells(Rows.Count, 1).End(xlUp).Row

    ReDim dd(aa)

    i = 1

    Range(Cells(2, 1), Cells(aa, 1)).Select

    Set bb = Selection

    For Each cc In bb

        dd(i) = cc.Value

        i = i + 1

    Next

    Sheets(50).Activate

    ff = 0

        For i = 1 To aa - 1

        ee = Range("D:D").Find(What:=dd(i), LookAt:=xlPart, LookIn:=xlValues, SearchOrder:=xlByColumns)

        On Error Resume Next

        If Len(ee) > 1 Then

        ff = ff + 1

        End If

    Next

    MsgBox ff


End Sub