missed ranges and the find function...

Engineer Joe

Well-known Member
Joined
Jun 10, 2005
Messages
543
hey you old crusty VBA veterans...here's an easy one for you...

I execute a find procedure on a worksheet, and try to name the range (lets call it "Body") that it finds...like so:

Code:
Set Body = .find(blah,blah,blah)

My question is: what happens when nothing is found? Body has already been defined as a range. But an error is not generated...Not until i try to use Body. I guess what i'm getting at is that i'd like to insert some code that either notifies me, or skips over any arguments that try to use that range after it misses. But I don't know how? Any tips? I love you. I mean, I would be appreciative...
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
See - you youngsters can't do without us. :p
Try this :-
Code:
Sub test()
    Dim FoundCell As Object
    Dim body As Range
    Set FoundCell = ActiveSheet.Cells.Find(what:="test")
    If FoundCell Is Nothing Then
        MsgBox ("Not found")
    Else
        Set body = FoundCell
        MsgBox (body.Address)
    End If
End Sub
------------------------------------------
I'm off for my afternoon nap now
'-----------------------------------------
 

Watch MrExcel Video

Forum statistics

Threads
1,118,087
Messages
5,570,139
Members
412,306
Latest member
fabio6
Top