You might want to zoom in and out of a worksheet using Excel VBA for several reasons. For example, if you have created a custom user interface, you might want to allow users to zoom in and out of a worksheet for a better user experience.
This tutorial shows how to zoom in and out of an Excel worksheet using Excel VBA.
How to Zoom in and Out of Worksheet Using Excel VBA
To zoom in and out of a worksheet in VBA, you can use the Zoom method of the Worksheet object. The Zoom method takes a percentage value as its argument, which specifies the zoom level. Note that the minimum zoom level in Excel is 10%, and the maximum is 400%.
We want to zoom in on the active worksheet by 50% and zoom out by 50% after five seconds. We use the below steps:
- On the active worksheet, press Alt + F11 to open the Visual Basic Editor.
- Open the Insert menu and choose Module to create a module.
- Copy the sub-routine below and paste it into the module.
' Zoom in
ActiveWindow.Zoom = ActiveWindow.Zoom + 50
' Wait for 5 seconds
Application.Wait Now + TimeValue("0:00:05")
' Zoom out
ActiveWindow.Zoom = ActiveWindow.Zoom - 50
- Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm).
- Place the cursor anywhere in the sub-routine and press F5 to run the code.
This code zooms in the active worksheet by 50%, waits for 5 seconds, and zooms out by 50%. Of course, you can change the zoom percentages and wait time per your requirement.
This tutorial showed that one of the reasons you might want to write a macro for zooming in and out of a worksheet using Excel VBA is to allow users to zoom in and out of a worksheet for a better user experience.
This tutorial showed how to zoom in and out of an Excel worksheet using Excel VBA. We hope you found the tutorial helpful.