If Cell A is not blank and Cell B is blank then......

O177812

Board Regular
Joined
Apr 16, 2015
Messages
93
Office Version
  1. 365
  2. 2021
I have a Macro that is checking a multitude of cells before submission of a form.
For a specific set of data I am trying to use the following:

ElseIf Range("B31") <> "" & Range("C31") = "" Then
MsgBox "Please enter a Sell Price for your first item"
Range("C31").Select

Basically, if the account manager has entered an item number but forgot to enter a price I want the macro to stop and a MsgBox reminder to appear.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You want something like this:
VBA Code:
If (Range("B31") <> "") And (Range("C31") = "") Then
    MsgBox "Please enter a Sell Price for your first item and try again!"
    Exit Sub
End If
 
Upvote 0
You want something like this:
VBA Code:
If (Range("B31") <> "") And (Range("C31") = "") Then
    MsgBox "Please enter a Sell Price for your first item and try again!"
    Exit Sub
End If

Thank you so much!.

I just encountered one additional challenge/sollution.
Instead of checking each cell in a row individually I believe it is possible to check all the cells.

ElseIf IsEmpty(Range("A31")) = False And Range("C31:P31") = "" Then
MsgBox "Your are missing some information for this item"
Range("C31").Select

Basically, without typing out a line for each individual cell. I want to check if there are any blank cells on row 31 between columns C & P I would like the macro to stop and the msg box appear.

Thank you in Advance!
 
Upvote 0
You can use the Excel COUNTA and COUNTBLANK functions to count how many cells in a range contain, or don't contain values.
You can use these functions in VBA by prefacing them with "Application.WorksheetFunction", i.e.
VBA Code:
Application.WorksheetFunction.COUNTA(...)
 
Upvote 0
So would it look something like this:

If IsEmpty(Range("A31")) = False And Application.WorksheetFunction.COUNTA(Range("B31:P31") >0 Then
MsgBox "Please enter a Quantity for your first item"

I'm a little rusty right now.
 
Upvote 0
If you want it to stop if there are any blank cells between B31 and P31, then I think you should be using COUNTBLANK, not COUNTA.
COUNTA counts if there is anything in the cell. If you want to check to see if there are any blanks in that range, you would either use:
COUNTBLANK > 0
or
COUNTA < 15
(since there are 15 cells in the range B31:P31)

Of course, the easiest way to see if it is working properly is to try it out for yourself, and see if it works!
Don't be afraid to try it out!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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