Prevent Printing if Two Cells are blank

sarahlynnette

New Member
Joined
May 14, 2014
Messages
6
Hi, I'm new to VBA and most of what I have learned has been through Google and I have found a lot on this site. I am trying to write a code that will prevent the user from printing the worksheet if two specific cells are blank. I have tried the following codes, none of which have worked. The first code didn't work due to once the first cell is completed it won't check the others and prevent printing. The second allows the worksheet to print regardless of whether it is blank or not. Could this be because I have a formula in those cells?

Private Sub Workbook_BeforePrint(Cancel As Boolean)
With Sheets("JULY")
If .Range("I2") = "" Then
.Select
.Range("I2").Select
MsgBox "Please enter a dollar amount into the Rate/Hr field"
If .Range("F40") = "" Then
.Select
.Range("F40").Select
MsgBox "Please enter time worked into the proper days"
End If
End With
End Sub


Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet.Name = "JULY" Then
If Application.WorksheetFunction.CountA(Sheets("JULY").Range("F40, I2")) < 2 Then
MsgBox "One or more of the required fields is blank."
Cancel = True
End If
End If
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try:

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
     With Sheets("JULY")
          If .Range("I2") = "" Or .Range("F40") = "" Then
               .Select
               .Range("I2").Select
               MsgBox "One or more of the required fields is blank"
               Cancel = True
          End If
     End With
End Sub
 
Upvote 0
Thanks, Red. But it still prints once I2 has a value in it and F40 is still blank. I need it to prevent printing if either is blank... Any other suggestions?
 
Upvote 0
That was the hold up!! I got it to work with your code and tweeking it a little with a less than sign rather than equal to. Thanks so much for your help :)
 
Upvote 0

Forum statistics

Threads
1,216,444
Messages
6,130,659
Members
449,585
Latest member
Nattarinee

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