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.
- To open the Visual Basic Explorer (VBE), right-click on the Order Form sheet tab, then click View Code.
- In the VBE, find the order form workbook in the list of projects, and under Microsoft Excel Objects, double-click ThisWorkbook.
- At the top left of the Code Window, select Workbook from the drop down list.
- 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.
- 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:
- Next, copy the code from the CustomerMessage macro, on the Module1, and paste it between the If and End If lines.
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.
- On the Order Form worksheet, delete the Customer Name in cell B5.
- 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
- 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