VBA help combining If value found in column range give Msgbox and End sub or continue sub if not found

Cmbuck

New Member
Joined
Mar 28, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, I brazenly copied the below macro section to check the range for the value "Not Allowed". And that works, except if none are found it still ends the sub. If none are found, I would like it to process the rest of the macro. Currently, it counts the number of occurrences "Not Allowed" happens and then give a message box. Ultimately, counting the "Not Allowed" is not necessary (it is nice to have, but not necessary), just want to make sure that if any instance of "Not Allowed" shows on the sheet, it does not run the macro and exits for the user to fix. If there are no instances of "Not Allowed" the macro continues to run. Here is where I am stuck. If "Not Allowed" is not found in the range, it just give a message box answer of "0 new item samples...." and ends the sub. Any help would be greatly appreciated. Thanks.

Dim rngData As Range
Dim rngCell As Range
Dim counter As Long: counter = 0
Set rngData = Range("AF38:AF" & Cells(Rows.Count, Range("AF38").Column).End(xlUp).Row)

For Each rngCell In rngData
If rngCell.Value = "Not Allowed" Then
counter = counter + 1
End If
Next rngCell

MsgBox counter & " New items samples are not allowed for this customer. Please remove item(s) and try again", vbCritical

[rest of macro follows here]
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the Board!

A simple IF...THEN clause should handle it, i.e.
VBA Code:
If counter > 0  Then
    Exit Sub
End If
 
Upvote 1
Solution
Apparently you are magic. I tried to put that in there previously and it was no cooperating, just kept ending the sub no matter what. I closed and reopened the file and it works. Thank you!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,947
Messages
6,175,560
Members
452,652
Latest member
eduedu

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