Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Printing All or Nothing.

Printing All or Nothing

Written by Allen Wyatt (last updated September 3, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


Karen is looking for a way to print an entire workbook, even if a user chooses to print a single worksheet. In other words, she is looking for a way to print either the entire workbook, or nothing at all—there should be no "in between" options.

The only way to handle this is through the use of a macro. VBA allows you to create macros that are initiated when certain events occur. One of the events that can trigger macros is the "print" event. When someone asks to print, or chooses to see a print preview, the BeforePrint event of the Workbook object is triggered. You can create your own macro that executes when the event is triggered.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim sht As Variant
    Dim bPreview As Boolean
    Dim iResponse As Integer
    On Error GoTo ErrHandler

    iResponse = MsgBox(prompt:="Do you want to Print Preview?", _
        Buttons:=vbYesNoCancel, Title:="Preview?")

    Select Case iResponse
        Case vbYes
            bPreview = True
        Case vbNo
            bPreview = False
        Case Else
            GoTo ExitHandler
    End Select
    Application.EnableEvents = False
    For Each sht In Sheets
        If sht.Visible Then
            sht.PrintOut Preview:=bPreview
        End If
    Next

ExitHandler:
    Application.EnableEvents = True
    Cancel = True
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

Whenever Excel gets ready to print, or whenever print preview is invoked, the BeforePrint event is triggered and this macro runs. The macro first asks the user if he or she wants to do a print preview. A Select Case structure is used to set the bPreview variable based on the answer to the question. The setting of bPreview then controls what happens.

If the user clicked Cancel when asked about previewing, then the macro is exited and the printing is canceled. Otherwise, each worksheet in the workbook is examined to either print or preview. If the worksheet is visible, it is printed, and the Preview property is set equal to bPreview (True means that the worksheet is previewed; False means it is actually printed).

Notice that the macro sets the EnableEvents property to False. This is done so that no other events can trigger while printing or previewing. If EnableEvents is left "on," then every time the PrintOut method is used, the entire BeforePrint event is again triggered—the user would end up in an endless loop if event handling were not turned off.

Also, note that one of the last things to occur before exiting the macro is that the Cancel property is set to True. This is done so that the original print or print preview request that generated the BeforePrint event is cancelled. There is, after all, no need to complete that request, as the macro did all the print handling for the user.

There is one caveat, of course, to using this approach to printing: If macros are not enabled, the handler will not run and the user can print as desired. (Holding Shift while opening the workbook disables macros and the user most times is asked if they want to enable macros.) Other issues of ExcelTips have discussed this fact.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11629) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Printing All or Nothing.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Calculating the Distance between the Top of the Window and Row 1

Normally Excel positions a UserForm in the center of your screen. You may want to position the form elsewhere, more ...

Discover More

Turning Off Smart Quotes for Specific Styles

Smart quotes can be helpful in making a great-looking document, but at times, they can be a real pain. Wouldn’t it be ...

Discover More

Capitalizing the First Letter after a Quote Mark

The AutoCorrect feature of Word will automatically capitalize the first letter of sentences as you type, unless the ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Specifying Print Quantity in a Cell

When you print a worksheet, you can specify in the Print dialog box how many copies you want printed. If you want the ...

Discover More

Forcing Worksheets to Print on a New Sheet

Excel allows you to easily print an entire workbook. Doing so, however, may not give the desired results if you a ...

Discover More

Repeating Rows on a Printout Except On the Last Page

When setting up a worksheet for printing, you can specify that Excel repeat some of your rows at the top of each page ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.