check if range does not contain certain text

ira daboy

New Member
Joined
Oct 10, 2013
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I am trying to see if a range of cells has the word "shipping" before closing and i keep getting an error. below is what i have. can anyone help me out?

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If WorksheetFunction.CountIf(Sheet2.Range("d2:d17") <> "shipping") Then
Cancel = False
MsgBox "Did you charge for shipping?"
End If
If Sheet6.Range("h1") = 2 And Sheet6.Range("f7") = "" Then
Cancel = True
MsgBox "You must enter an invoice date and a PO#."
End If

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You have not stuctured the COUNTIF formula correctly. It has two arguments.
I think you want it structured something like this:
Rich (BB code):
If WorksheetFunction.CountIf(range,"*shipping*")=0 Then
 
Upvote 0
Hi Joe, I still get the same error when I use your syntax. Also how do I refer to a range on another sheet. Thanks for you help
 
Upvote 0
Hi Joe, I still get the same error when I use your syntax. Also how do I refer to a range on another sheet. Thanks for you help
Please post the updated version of the code you tried, and tell us exactly what the error message is.

Also, you are already prefacing your ranges with the sheet references. That is one way to reference other sheets (supposing you are using the correct value).
 
Upvote 0
Here is my code: I just replaced the second line with what you suggested and the attached is the error i get. thanks

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If WorksheetFunction.CountIf(Range, "*shipping*") = 0 Then
Cancel = False
MsgBox "Did you charge for shipping?"
End If
If Sheet6.Range("h1") = 2 And Sheet6.Range("f7") = "" Then
Cancel = True
MsgBox "You must enter an invoice date and a PO#."
End If
End Sub
 

Attachments

  • error1.JPG
    error1.JPG
    15.6 KB · Views: 2
Upvote 0
OK, you misinterpretted my initial reply.
I was posting how the statement was to be structured. Note how I put "range" in italics.
That was meant to just be a place holder for your actual range.
You need to enter/substitute a valid range in that spot (whatever range you actually want to run the COUNTIF function against).
 
Upvote 0
Sorry to be a pain but this just isn't working for me. Below is my new code and I still get the same error. As well what I am trying to do is see if the word "shipping" doesn't" exist then remind me to charge for shipping.
thanks for you help with this.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If WorksheetFunction.CountIf(Sheet2.Range, "d2:d17") = 0 Then
'Cancel = False
MsgBox "Did you charge for shipping?"
End If
If Sheet6.Range("h1") = 2 And Sheet6.Range("f7") = "" Then
Cancel = True
MsgBox "You must enter an invoice date and a PO#."
End If
End Sub
 
Upvote 0
What did you do here?
Rich (BB code):
If WorksheetFunction.CountIf(Sheet2.Range, "d2:d17") = 0 Then
Why did you split your range up over two arguments, and totally get rid of the criteria?

Isn't this what you want?
Rich (BB code):
If WorksheetFunction.CountIf(Sheet2.Range("d2:d17"), "*shipping*") = 0 Then
 
Upvote 0
Solution
You are welcome.
Glad we got it all working the way you need.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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