[英]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.