在Excel中查找方法引发错误91

时间:2014-10-30 10:52:37

标签: excel excel-vba vba

我在Excel宏中使用find方法搜索单元格中的VLOOKUP,我的目标是我需要知道哪个公式不包含VLOOKUP,我的方法运行良好,直到那里有一个牢房 没有VLOOKUP和宏继续调试运行时错误' 91'

我的问题是我应该如何正确地编写宏,所以我不会得到调试直到activecell 包含*,下面是我的宏:

Sub findvlookup()

    Do While Not ActiveCell.Value = "*"

        If Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
           LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
           MatchCase:=False, SearchFormat:=False).Activate Then

            ActiveCell.Offset(1, 0).Select

        End If

    Loop

End Sub

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

你不需要循环来找到你的第一次出现的内容:什么:="串

Dim fndVL as Range

fndVL = Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Not fndVL is Nothing Then
   MsgBox "Found it"
Else
   NsgBox "Not found"
End If

如果您想查找后续实例,请查看FindNext,并可能在循环中使用它。

答案 1 :(得分:0)

Sub findvlookup()

Dim rngFind As Range

Do While Not ActiveCell.Value = "*"

    Set rngFind = Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        If Not rngFind Is Nothing Then

            ActiveCell.Offset(1, 0).Select

        Else
            Exit Sub
        End If



Loop

End Sub

答案 2 :(得分:0)

您正在使用Find方法,技术详细信息为Here

您可以找到(例如):

  

Common Err.Numbers(9,91):指定的文字不在目标工作簿中。

如果您的单元格有错误值,则会出现其他类型的错误:例如#Value