Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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: Automatic Selection of Portrait or Landscape.
by Allen Wyatt
(last updated October 25, 2014)
Barbara has a worksheet that she needs to print out periodically. Sometimes she needs to only print three columns of data and other times she needs to print more. When she prints only three columns they fit very nicely on a page printed in portrait orientation. When she prints more columns then she needs to print in landscape orientation. Barbara wonders if there is a way she can set up Excel so that it automatically switches from portrait to landscape based upon the number of columns she wants to print.
Perhaps the easiest way to handle this type of printing is to add a little macro that runs just before printing. If the print area is set so that it contains 1, 2, or 3, columns, then the printout is done in portrait orientation. Any other number of columns and landscape orientation is used. Here is the macro; you should add it to the ThisWorkbook module:
Private Sub Workbook_BeforePrint(Cancel As Boolean) With ActiveSheet.PageSetup If Range(.PrintArea).Columns.Count > 3 Then .Orientation = xlLandscape Else .Orientation = xlPortrait End If End With End Sub
Of course, it may be more beneficial (and flexible) if you simply use the Custom Views feature of Excel. You can specify a view that includes your three columns or any number of columns you want. You can even have the view include print settings, so the orientation of the page would be included in the view. Follow these steps to set up the views:
Figure 1. The Add View dialog box.
Now, whenever you want to print different ways, you just call up the view you want and choose to print—everything else is already set for you.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10365) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Automatic Selection of Portrait or Landscape.
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!
If you need to modify where a worksheet is printed (meaning, which paper tray it should use), Excel doesn't provide a lot ...Discover More
Need to print just a few pages from a group of worksheets? The easiest way to handle the task may be through a macro, as ...Discover More
Would you like to have a worksheet automatically printed when a particular cell contains a specified value? You can ...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 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.