Error Handling if value is not found in search

RLJ

Active Member
Joined
Mar 15, 2011
Messages
417
Office Version
  1. 365
Platform
  1. Windows
I'm having problems getting the below code to work properly, I can't seem to get the msgbox to only show when the value is not found. It show every time I run the code.


VBA Code:
Sub FindEvent()
    Dim Evnt As String, Found As Range
    
    Evnt = Range("B2").Value

    If Found Is Nothing Then
        MsgBox "Event does not have a cheat sheet in the workbook", vbExclamation, "Not Found"
    Else
        With ActiveSheet
            .Range("B" & .Range("SearchRng").Find(Evnt).Row).Select
        End With
        With Range("B2:C2")
            .ClearContents
        End With
    End If

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You need to include a Range.Find statement before you test variable Found. Example:

VBA Code:
Set Found = ActiveSheet.Range("B1:B10").Find(What:="BLACK", LookAt:=xlPart)

The way it is now, Found will always = Nothing and the MsgBox will always appear.
 
Upvote 0
Solution
You need to include a Range.Find statement before you test variable Found. Example:

VBA Code:
Set Found = ActiveSheet.Range("B1:B10").Find(What:="BLACK", LookAt:=xlPart)

The way it is now, Found will always = Nothing and the MsgBox will always appear.
Thank you. It fixed my problem.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top