Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find if `find` method returns `nothing` in excel vba

I'm trying to find an id in a list and get it's address, but also deal with a situation if nothing is found.

Here's what I have:

Function find_in_two_ranges_two_sheets(ws1 As String, col1 As Integer) As Range

    Dim rows1 As Integer
    rows1 = Get_Rows_Generic(ws1, 1)
    Dim range1 As Range ' range of first search
    With Worksheets(ws1)
        Set range1 = .Range(.Cells(1, col1), .Cells(rows1, col1))
    End With

    Dim found1 As Range
    Set found1 = range1.Find("test id", LookIn:=xlValues)  

    If found1 = Nothing Then
        MsgBox "nothing"
    Else
        MsgBox found1.AddressLocal
    End If


    Set find_in_two_ranges_two_sheets = range1
End Function


Sub test_stuff()
    Dim x As Range
    Set x = find_in_two_ranges_two_sheets("usersFullOutput.csv", 1)
    MsgBox x.Address
End Sub

When I run test_stuff() I get an error in the function in the line If found1 = Nothing Then with the word Nothing highlighted. "Compile error; Invalid Use of Object". Not sure what to do.

like image 302
DBWeinstein Avatar asked Mar 15 '15 21:03

DBWeinstein


People also ask

What does find function in VBA return?

Define the FIND function of VBA Excel. One can specify the direction of search, order of search, data to be searched, the format of the search value, and so on. The FIND function returns the cell containing the specified value. If a match is not found, the function does not return anything.

What does Range find return if nothing is found?

This method returns Nothing if no match is found. The Find method does not affect the selection or the active cell. The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method.

How do you check if a cell is empty or not in VBA?

To check if a cell is empty you can use VBA's ISEMPTY function. In this function, you need to use the range object to specify the cell that you want to check, and it returns true if that cell is empty, otherwise false. You can use a message box or use a cell to get the result.

How do you find a value in an Excel column by VBA code cells find?

To find a cell with a numeric value in an Excel Table, set the SearchDirection parameter to either of the following, as applicable: xlNext (SearchDirection:=xlNext): To search for the next match. xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.


1 Answers

To check the range object you need to use is instead of =:

If found1 Is Nothing Then
    MsgBox "nothing"
Else
    MsgBox found1.AddressLocal
End If

Explanation:

Taken from Allen Browne

Nothing is the uninitialized state of an object variable. An object cannot be a simple variable such as a number or a string, so it can never be 0 or "". It must be a more comprehensive structure (a text box, form, recordset, querydef, ...)

Since it is not a simple value, you cannot test if it is equal to something. VBA has an Is keyword that you use.

like image 128
tospig Avatar answered Sep 28 '22 19:09

tospig