Display Error Message and Stop VBA Process when Text Not Found

Godlewskij

New Member
Joined
Jul 26, 2010
Messages
2
I've got an Excel tool I built to analyze reports. The tool works fine, but after it finishes deleting the info I'm not interested in, it occasionally ends up with no findings. This is good, but confusing to some others who may need to use my tool. What I would like to do is enter new VBA Code at a certain point that will search for the text "XYZ" in column A. If "XYZ" is found, then continue processing. However, if "XYZ" is not found, I want a message displayed "There are no XYZ's found in the report" and then have the VBA process stop at that point.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Dim Found As Range
Set Found = Columns("A").Find(what:="XYZ", LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "XYZ not found", vbExclamation
    Exit Sub
End If
 

Godlewskij

New Member
Joined
Jul 26, 2010
Messages
2
VoG, Thanks. Worked like a charm. This is why I love the MrExcel website. Been using it for years and the two times I haven't been able to figure something out, someone if the forum has been able to help.

Thanks Again. Joe
 

Watch MrExcel Video

Forum statistics

Threads
1,132,739
Messages
5,655,023
Members
418,171
Latest member
ramiroayala

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
Top