This post may contain affiliate links. Please read my disclosure for more info.

VBA Code to control Excel’s Zoom settings

VBA Code Snippets

VBA Code Snippets

Excel has zoom settings for worksheets, UserForms and print settings.  Each of these serves different purposes and used at different points, but they can all be controlled by VBA.


Worksheet zoom settings

The worksheet zoom setting in Excel will always be an integer.  If the value set by VBA contains decimal places it will be rounded down to the nearest whole number.

'Change worksheet zoom setting for the active window
ActiveWindow.Zoom = 56
'Change worksheet zoom setting for any open window
Windows("Book1.xlsx").Zoom = 56

The minimum and maximum zoom settings permitted are 10% and 400%.


Zoom to Selection

Excel contains a setting called Zoom to Selection, which is in the View Ribbon.

View Zoom To Selection

Clicking this button will automatically zoom the active worksheet to fit the selected cells.   This option is also available within the Zoom window.

Zoom settings Fit to Selection

The Zoom to Selection / Fit to Selection feature can be controlled with VBA, however it does require a range to be selected.  The code below selects Cells A1:J15, the and zooms the window to the size of those cells.

'Zoom to selection
ActiveWindow.Zoom = True


Transferring current zoom setting to a variable

The zoom setting can be assigned to a variable for use at a later point in the code.

'Setting variable to zoom setting of active window
Dim zoomSetting As Integer
zoomSetting = ActiveWindow.Zoom
'Setting variable to zoom setting of any named window 
Dim zoomSetting As Integer 
zoomSetting = Windows("Book1.xlsx").Zoom


Change the zoom functions of the mouse scroll wheel


Virtually every mouse has a scroll wheel, or similar scrolling feature.  By default the wheel will scroll up and down the page, but with Ctrl + mouse scroll will zoom into an Excel worksheet.  With VBA it is possible to reverse these settings when used within Excel.

'Zoom with souse scroll, scroll with Ctrl + mouse scroll
Application.RollZoom = True
'Zoom with Ctrl + mouse scroll, scroll with mouse scroll (default settings)
Application.RollZoom = False


The Zoom DialogBox

VBA can trigger the display of the Zoom window.

The value after the .Show is the default size selected in the window.  If that value is also a standard size, such as 200%, 100%, 75%, etc., that option will be highlighted automatically in the Zoom window.

'Display Zoom window include 150% as default selection
Application.Dialogs(xlDialogZoom).Show (150)

I do not believe it is possible to capture the requested value from the Zoom window, without changing the zoom setting.  Therefore, as a workaround, it is necessary assign the original value to a variable, change the zoom setting, record the new value, then revert to the original.

'Capture Zoom Setting from dialogBox

'Create variable to hold the zoom setting
Dim zoomSetting As Integer
zoomSetting = ActiveWindow.Zoom

'Open the zoom dialog box to change setting
Application.Dialogs(xlDialogZoom).Show (zoomSetting)

'Print Zoom Setting to the Immediate Window
Debug.Print ActiveWindow.Zoom

'Return the Zoom Setting to original state
ActiveWindow.Zoom = zoomSetting


Print Zoom

When printing a document there are different zoom settings available, which only apply to printed documents.

Zoom Settings - Page Setup

This setting is controlled by the following VBA examples.  Each example is applied to a worksheet called ZoomSettings, but could also be applied to the ActiveSheet.

'Set the zoom level
Worksheets("ZoomSettings").PageSetup.Zoom = 150
'Turn off the zoom level and fit to pages
Worksheets("ZoomSettings").PageSetup.Zoom = False
'Zoom to a specific number of pages
Worksheets("ZoomSettings").PageSetup.FitToPagesWide = 5
Worksheets("ZoomSettings").PageSetup.FitToPagesTall = 1


UserForm Zoom

The VBA UserForm also includes it’s own zoom setting.  Though, it’s not as useful as you might think, as it does to change the size of the form, just the items on the form.  To keep things in proportion, it is necessary to resize the form using the Height and Width properties.

'Settings below this size will change the proportions of the UserForm to 300%
Dim UserFormZoom As Double
ZoomSetting = 300

'Display the UserForm

'Change zoom setting of UserForm
UserForm1.Zoom = ZoomSetting

'Change Width & Height by same proportion as Zoom setting
UserForm1.Width = UserForm1.Width * ZoomSetting / 100
UserForm1.Height = UserForm1.Height * ZoomSetting / 100

The minimum and maximum permitted widths are 99 and 12287.5.

The minimum and maximum permitted heights are 28.5 and 12287.5.

If provided with a value outside the permitted range, VBA will automatically adjust the height and width UserFrom to be minimum or maximum.  This can result in UserForms which are not in the same proportions.

Want to Learn VBA & Macros?

If you want to automate Excel and save time, my recommended resouces for learning VBA Macros are: