VBA MSGBOX For each instance error

jamesmev

Board Regular
Joined
Apr 9, 2015
Messages
233
Office Version
  1. 365
Platform
  1. Windows
I currently have the following vba
VBA Code:
 Dim SrchRng As Range, cel As Range
    Set SrchRng = Sheets("Brand").Range("J:J")
    For Each cel In SrchRng
        If InStr(1, cel.Value, "Material") > 0 Then
            MsgBox "There is a error which needs to be keyed at lower level. Please filter sheet."
        End If
However, if there are multiple instances of "Material" in J it produces a pop up for each instance. I Just need it to pop up once. Not for each instance.
Any help?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
When posting code please post all the code. I know it seems like you have shown only the part of code necessary, but I am going to give you a solution that could possibly break your code depending on what the rest of your code looks like. Also I suggest an error message that tells the user what the error actually is.

Replace all code shown with this. Also remove the "Next" statement, which you did not show.

Rich (BB code):
    If Not Sheets("Brand").Range("J:J").Find(what:="Material", lookAt:=xlPart, lookin:=xlValues) Is Nothing Then
            MsgBox """Material"" found in column J. This is an error which needs to be keyed at lower level. Please filter sheet."
   End If
 
Upvote 0
Try
VBA Code:
Dim blnIsError as Boolean
'...
blnIsError = False
For Each cel In SrchRng
       blnIsError = blnIsError Or (InStr(1, cel.Value, "Material") > 0)
Next cel

If blnIsError Then MsgBox "There is a error which needs to be keyed at lower level. Please filter sheet."
 
Upvote 0

Forum statistics

Threads
1,214,406
Messages
6,119,330
Members
448,888
Latest member
Arle8907

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