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.

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.

  1. To open the Visual Basic Explorer (VBE), right-click on the Order Form sheet tab, then click View Code.
    Excel View Code
  2. In the VBE, find the order form workbook in the list of projects, and under Microsoft Excel Objects, double-click ThisWorkbook.
    Excel VBE ThisWorkbook
  3. At the top left of the Code Window, select Workbook from the drop down list.
    Excel VBE Workbook
  4. 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
  5. 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
  6. 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.

  1. In the Workbook_BeforePrint code, add a blank line, after the MsgBox line.
  2. In the blank line, type: Cancel=true
  3. 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:


You may also like...

4 Responses

  1. James says:

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

  2. Contextures Blog » Excel VBA Video: Show Message Before Printing says:

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

  3. V V N RAO says:

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

  4. Nadine says:


    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


Leave a Reply

Your email address will not be published. Required fields are marked *