繁体   English   中英

Excel VBA查找字符串:错误2015

[英]Excel VBA find string : Error 2015

我必须遵循代码片段...

  Public Sub FindText(path As String, file As String)
    Dim Found As Range

    myText = "test("

    MacroBook = ActiveWorkbook.Name

    ' Open the File
    Workbooks.Open path & file, ReadOnly:=True, UpdateLinks:=False
    For Each ws In Workbooks(file).Worksheets
     With ws

       Set Found = .UsedRange.Find(What:=myText, LookIn:=xlFormulas, _
                      LookAt:=xlPart, MatchCase:=False)

       If Not Found Is Nothing Then
        ' do stuff
        ' ...

我在调试器中看到Found包含Error 2015! 工作表包含公式中我想要的文本。

任何想法为什么我收到错误?

谢谢

从评论到Q的跟进,出现Error 2015 ,因为表单中的公式返回#VALUE! 错误。 您可以使用IsError处理它:

If Not Found Is Nothing Then
    If Not IsError(Found) Then
       ' do sth
    End If
End If

您不需要在代码中使用“设置”。 您只能使用它来指定对象的引用。 尝试:-

For Each ws In Workbooks(file).Worksheets
     With ws

       Found = .UsedRange.Find(What:=myText, LookIn:=xlFormulas, _
                      LookAt:=xlPart, MatchCase:=False)

       If Not Found Is Nothing Then
        ' do stuff
        ' ...

希望这应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM