Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Printing Based on Cell Contents.
by Allen Wyatt
(last updated June 4, 2015)
Theresa wonders if there is a way to format a cell so that if the contents of the cell meet certain criteria then a specific worksheet is automatically printed. The short answer is no, there is no way to use formatting to achieve this goal. You can, however, use an event handler macro to do the printing.
For example, one of the event handlers supported by Excel is triggered every time something in the workbook is changed. You can create an event handler that examines which cell was changed. If it is a specific cell, and if that cell contains a particular value, then a worksheet can be printed.
Private Sub Worksheet_Change(ByVal Target As Range) Dim targCell As Range Set targCell = Worksheets(1).Range("B2") If Not Application.Intersect(Target, targCell) Is Nothing Then If targCell.Value = 1001 Then Worksheets(1).PrintOut End If End If End Sub
This macro examines the contents of cell B2. If the cell contents are changed and if the cell contains the value 1001, then the worksheet is automatically printed.
Of course, you may want the contents of a particular cell to control what is printed when someone actually chooses to print. For instance, if the user chooses to print, you may want to examine the contents of a cell (such as E2) and, based on the contents of that cell, automatically modify what is printed. The following macro takes this approach:
Private Sub Workbook_BeforePrint(Cancel As Boolean) Application.EnableEvents = False Select Case Worksheets("Sheet1").Range("E1") Case 1 Worksheets("Sheet1").PrintOut Case 2 Worksheets("Sheet2").PrintOut Case 3 Worksheets("Sheet3").PrintOut Case 4 Worksheets("Sheet4").PrintOut Case Else ActiveSheet.PrintOut End Select Cancel = True Application.EnableEvents = True End Sub
The macro prints Sheet1, Sheet2, Sheet3, or Sheet4 depending on whether cell E2 contains 1, 2, 3, or 4.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3832) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Printing Based on Cell Contents.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When you print a worksheet, you can specify in the Print dialog box how many copies you want printed. If you want the print ...Discover More
Need to have your print settings always be a certain way? Tired of resetting the settings after others use the workbook or ...Discover More
Need to print a portion of a worksheet, but don't want to waste paper by printing the whole thing? It's easy to print just ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.