Yes, you can automate this in Excel VBA using the PageSetup object and the FitToPagesWide property. This approach forces the active worksheet to scale horizontally so that all columns fit on a single printed page, regardless of how many columns are in the data range. It requires no external libraries or add-ins, just a standard Excel installation with the Developer tab enabled. Below is a practical guide to setting this up correctly and troubleshooting common issues that arise when scaling data.
What You'll Need
- Microsoft Excel: The macro will run on any version of Excel that supports VBA (2010, 2013, 2016, 2019, Office 365, and Excel for Mac). The code is identical across platforms, though the keyboard shortcut to open the VBA editor differs slightly.
- A Worksheet with Data: You need a sheet containing the columns you wish to print. The code works on the active sheet, so ensure the correct tab is selected before running the macro.
- Developer Tab Access: You must be able to open the Visual Basic Editor. If this tab is hidden, go to File > Options > Customize Ribbon and check the box next to Developer in the right-hand pane.
Step-by-Step
- Open the Visual Basic Editor. Press Alt + F11 on Windows or Command + F11 on Mac to launch the editor. If you prefer using the ribbon, click the Developer tab on the main Excel ribbon and select Visual Basic in the Code group.
- Insert a New Module. In the VBA editor window, look at the left-hand project explorer pane. Right-click on your workbook name (e.g., Book1) and select Insert > Module from the context menu. This creates a blank code window where you will type your script.
- Copy and Paste the Code. Paste the following VBA code into the blank module window. This script selects the entire used range of the active sheet and applies the fit-to-page settings.
Sub FitAllColumnsToOnePage()
Dim ws As Worksheet
Dim rng As Range
Set ws = ActiveSheet
Set rng = ws.UsedRange
With ws.PageSetup
' Turn off automatic scaling to prevent conflicts
.Zoom = False
' Fit all columns to one page wide
.FitToPagesWide = 1
' Let Excel determine the number of pages tall automatically
.FitToPagesTall = False
End With
MsgBox "Columns have been set to fit on one page.", vbInformation
End Sub
- Save the Macro. Before exiting the editor, it is good practice to save the project. Press Ctrl + S to save the file. Ensure you save it as an Excel Macro-Enabled Workbook (.xlsm) if you intend to use the macro in the future. If you save it as a standard .xlsx file, the macro code will be stripped out automatically.
- Return to Excel. Close the VBA editor window or click the Excel icon in the toolbar to return to your spreadsheet.
- Run the Macro. First, click on the specific worksheet tab you want to print. If you want to print the whole workbook, you would need a slightly different loop-based script. For now, ensure the tab is active. Press Alt + F8 to open the Macro dialog box. You should see
FitAllColumnsToOnePagelisted. Click on it once and then click the Run button. - Verify the Print Preview. After the macro runs, you will see a pop-up message. To confirm the settings were applied correctly, press Ctrl + P or go to File > Print. Check the Settings pane. The option should now read Fit Sheet on One Page, and the preview should show all your columns compressed horizontally to fit within the margins.
Customizing the Code
The basic script above is a powerful starting point, but you can modify it to suit specific reporting needs. For instance, if you only want to fit the first 10 columns and leave the rest on a second page, you can alter the range selection logic. Alternatively, you might want to apply this to every sheet in the workbook using a loop. Below are two common variations.
Variation 1: Fit Specific Columns Only
If your data has a header row that is always visible, but the data body changes in length, you might want to fit the header and the first 15 data columns. Modify the Set rng line in the previous code to point to a specific range:
' Example: Fit columns A to O (15 columns) on one page
Set rng = ws.Range("A1:O" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
Variation 2: Apply to Every Sheet in the Workbook
To ensure every tab in the file follows the same print layout, replace the main body of the macro with this loop structure: