VBA - Message box if specific cell isn't populated

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
126
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have a macro that does something pretty simple - prints one sheet to PDF and saves another sheet in a new workbook. I've basically automated a payment workbook using a bunch of formulas, but one of the cells is a dropdown that must be manually populated.

I would like something at the very beginning of the code where a message box would pop up and essentially kill the macro if cell E2 is blank.
Code:
Sub CheckPmtType()

If Range("E2").Value = ”” Then MsgBox "Please select a payment type"

Else Call PrintToPDF

End Sub

I’m sure I just need to add something about exiting the sub after the if/then statement but I’m stuck.

Thank you!!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The Else will not work if the entire IF...THEN is placed on one line.
Also, VBA does not like slanted double-quote marks like ”.

Try:
VBA Code:
Sub CheckPmtType()

If Range("E2").Value = "" Then
    MsgBox "Please select a payment type"
Else
    Call PrintToPDF
End If

End Sub
 
Upvote 1
Solution
The Else will not work if the entire IF...THEN is placed on one line.
Also, VBA does not like slanted double-quote marks like ”.

Try:
VBA Code:
Sub CheckPmtType()

If Range("E2").Value = "" Then
    MsgBox "Please select a payment type"
Else
    Call PrintToPDF
End If

End Sub

Works perfectly. Thanks so much!!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 1

Forum statistics

Threads
1,223,710
Messages
6,174,019
Members
452,542
Latest member
Bricklin

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