Excel VBA查找string:错误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 ' ... 

我在debugging器中看到发现包含错误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 ' ... 

希望这应该工作。