Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA find string : Error 2015

Tags:

find

excel

vba

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! The sheet contains the text I want in the formula.

Any ideas why I'm getting the error?

Thanks

like image 531
Rueful Rabbit Avatar asked Feb 25 '14 12:02

Rueful Rabbit


People also ask

How do I find a string in Excel VBA?

INSTR Function Using VBA to Find String in a Cell You can find any string and its position from any cell. The VBA InStr Function indicates if a text string is detected in another text string. It returns 0 if the text is not found. Otherwise, it returns the character position where the text is located.

Is there a Find function in VBA?

The FIND function of VBA searches for a specified value in the range defined by the user. To search, a VBA code is written by entering some or all arguments of the FIND function. One can specify the direction of search, order of search, data to be searched, the format of the search value, and so on.

How do I use InStr in VBA?

The syntax of VBA InStr is “InStr([start],string1,string2,[compare]).” In comparison, the syntax of InStrRev is “InStrRev(string1,string2,[start,[compare]]).” If the “start” argument is omitted, the InStr begins to search from the starting (first position) of the string.

How do I find a character in a string VBA?

InStr function finds the position of a specified substring within the string and returns the first position of its occurrence. For example, if you want to find the position of 'x' in 'Excel', using the Excel VBA InStr function would return 2.


1 Answers

As follow up from comments to the Q, Error 2015 occurs because your formula in the sheet returns #VALUE! error. You can handle it using IsError:

If Not Found Is Nothing Then
    If Not IsError(Found) Then
       ' do sth
    End If
End If
like image 154
Dmitry Pavliv Avatar answered Oct 20 '22 13:10

Dmitry Pavliv