简体   繁体   English

Excel VBA查找字符串:错误2015

[英]Excel VBA find string : Error 2015

I have to following code snippet ... 我必须遵循代码片段...

  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
        ' ...

I see in the debugger that Found contains Error 2015! 我在调试器中看到Found包含Error 2015! The sheet contains the text I want in the formula. 工作表包含公式中我想要的文本。

Any ideas why I'm getting the error? 任何想法为什么我收到错误?

Thanks 谢谢

As follow up from comments to the Q, Error 2015 occurs because your formula in the sheet returns #VALUE! 从评论到Q的跟进,出现Error 2015 ,因为表单中的公式返回#VALUE! error. 错误。 You can handle it using IsError : 您可以使用IsError处理它:

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

You don't need to use 'Set' in your code. 您不需要在代码中使用“设置”。 You only use this to assign a reference to an object. 您只能使用它来指定对象的引用。 Try:- 尝试:-

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
        ' ...

Hopefully this should work. 希望这应该工作。

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

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