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.
Clicking this button will automatically zoom the active worksheet to fit the selected cells. This option is also available within the Zoom window.
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 Range("A1:J15").Select 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
When printing a document there are different zoom settings available, which only apply to printed documents.
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
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 zoomSetting As Double ZoomSetting = 300 'Display the UserForm UserForm1.Show '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.
About the author
Hey, I’m Mark, and I run Excel Off The Grid.
My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.
In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).
Do you need help adapting this post to your needs?
I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.
But, if you're still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise. List all the things you've tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don't go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: