Excel VBA: Show a Message Before Printing

A bit of Excel programming can help us save some time and paper. Our goal is to stop people from printing an order form if they forgot to enter a customer name.

Final Macro Changes

Last week, you created a macro to show a message if the customer name was missing in an order form.

Today, you’ll make a final change to the macro, so it will run automatically when someone tries to print the order form.

Sample File: You can download the Excel order form with last week’s message macro. Enable macros when the file opens.

Create the Event Code

To create Excel code that runs automatically, you can use Event procedures. Those are macros that run when something specific happens in Excel, such as changing a cell on a worksheet, or printing a worksheet.

  • To open the Visual Basic Explorer (VBE), right-click on the Order Form sheet tab, then click View Code.
Create the Event Code
Create the Event Code

In the VBE, find the order form workbook in the list of projects, and under Microsoft Excel Objects, double-click ThisWorkbook.

Excel VBE ThisWorkbook

  • At the top left of the Code Window, select Workbook from the drop down list.

Excel VBE Workbook

  • An event procedure for Workbook_open is automatically created, but that’s not the one we need, so just ignore it. At the top right of the Code Window, select BeforePrint from the drop down list.

Excel VBE Before Print

  • The cursor should be flashing between the Sub and End Sub lines for the Workbook_BeforePrint macro. We want to check the order form before someone prints it, so type the following 2 lines of code, to check the name of the active sheet:

VBE Before Print 02

  • Next, copy the code from the CustomerMessage macro, on the Module1, and paste it between the If and End If lines.

VBE Before Print 03

Now, when someone tries to print or print preview, Excel will check the sheet name.

  • If OrderForm is the active sheet, it will check for a customer name in cell B5.
  • If the customer name is missing, our warning message will appear.

Test the Event Code

To see if the BeforePrint code is working correctly, you can test the code by using Print Preview.

  1. On the Order Form worksheet, delete the Customer Name in cell B5.
  2. Click the Office Button, point to Print, then click Print Preview.

The good new is that the warning message appears, because the Customer Name cell is empty. The bad news is that the Print Preview opens, even though the customer name is missing.

Fine-Tune the Event Code

You can see “(Cancel as Boolean)” at the end of the first line in the Workbook_BeforePrint macro. Some Excel events have a Cancel option, which allows you to cancel the event, if necessary.

We’ll add one more line of code, to cancel the printing if the customer name is missing.

  • In the Workbook_BeforePrint code, add a blank line, after the MsgBox line.
  • In the blank line, type: Cancel=true

VBEBeforePrint04

  • Then, save the workbook, and try the print preview again.

Now the warning message should appear if the customer name is missing, and the print or print preview should be cancelled.

If the customer name is filled in, then no message will appear, and the order form will print, or show up in the print preview window.
_________________

Previous Excel VBA articles:

Getting Started With Excel VBA

Excel VBA: Edit Your Recorded Macro

Excel VBA: Show a Message to Users

Excel VBA: Show Message Automatically

_________________

5 thoughts on “Excel VBA: Show a Message Before Printing”

  1. very helpful… as most of the times we face this problem and had to print again…. thank you for programming code.

  2. […] you saw instructions for automatically showing an error message before printing, if data is missing from an Excel […]

  3. I have created this programme but VB code is not working even Cell B5 is blank print option works and no msg dispalys

  4. Hello
    This is so helpful, but have to also have a message box if other cells are also missing e.g. city visited, etc. How did I link the code together in the Workbook_Before Print code?
    Many thanks
    Nadine

  5. Not sure what I am doing wrong… I am running office 365 and i did everything that I was instructed and it did not work. So, I downloaded the form and used the form to perform a test and the form I downloaded did not even work. I checked to make sure that Macros was turned on and they are. Any ideas?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.