VBA – Zoom in and Out of Worksheets

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on March 14, 2024

ActiveWindow.Zoom

You can use VBA to change the Zoom of a worksheet. Here’s code to change the Zoom of the ActiveWindow to 50%:

ActiveWindow.Zoom = 50

Change Zoom on all Worksheets

You can also loop through all the worksheets in your workbook to set a standard Zoom. The following Macro will set the Zoom for all worksheets to 50%:

Sub ZoomAll()

  Dim ws As Worksheet

  Application.ScreenUpdating = False

  For Each ws In Worksheets
    ws.Activate
    ActiveWindow.Zoom = 50
  Next

  Application.ScreenUpdating = True

End Sub

Zoom – Fit Selection

In a Worksheet you can use View->Zoom->Fit Selection to automatically adjust the Zoom to fit a selected Range.

You can “Fit Selection” in VBA by selecting the Range and setting Zoom equal to True.



Sheet1.Range("A1:F15").Select 'set range zoom
ActiveWindow.Zoom = True

Zoom Zoom

And finally a magically growing worksheet. The following macro will loop through the Zooms for Sheet1, going from 10% to 200%, incrementing by 10%, pausing a second between changes, and then it will restore Sheet1 back to it’s original state.

Sub ZoomZoom()
Dim x As Integer 'variable for loop
Dim OriginalZoom As Integer 'variable for original zoom

Sheet1.Activate 'let's work with sheet1

OriginalZoom = ActiveWindow.Zoom 'get current zoom

'loop through zoom 10 to 200 by 10
    For x = 1 To 20
        ActiveWindow.Zoom = x * 10
        Application.Wait Now + TimeValue("00:00:01")
    Next x
    
'restore original zoom
ActiveWindow.Zoom = OriginalZoom

End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

 

Learn More!

 

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples