Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Code to Force a certain zoom level

Tags:

excel

vba

I have a Validated Dropdown list in Excel that is unreadable if zoom is less than 100. I checked on the internet and fvound that I can not alter the size of the Validated list text size so I want to enforce a set zoom of 100.

I have the code to do this as follows

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveWindow.Zoom = 100
End Sub

This works and is fine for people who use zoom less than 100, but if people use a greater than 100 zoom it restricts the zoom to 100. Is there a way to overcome this, something along the lines of an If-Else statement.

If zoom less than 100 then zoom = 100 else if zoom greater than 100 do nothing

Thanks.

like image 439
Ckeane Avatar asked Jan 30 '12 10:01

Ckeane


People also ask

How do I change the zoom level in VBA?

Change the zoom functions of the mouse scroll wheel 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.

How do I lock the zoom in Excel?

You can do this by right-clicking on an empty space of the status bar, which displays a Context menu. At the bottom of the Context menu, click on the Zoom Slider option. This should remove the check mark from that option and, in the process, remove the Zoom slider from the status bar.

Can you do a nested if in VBA?

In VBA, you can use one IF statement inside another IF statement to create nested IFs. In simple words, you can execute one test statement using IF based on the result of another IF statement. In this kind of conditional statement, you need to test complex conditions.

How do I run a macro from a certain point?

To begin single stepping while a macro is running, press CTRL+BREAK. To begin single stepping at a specific point in your macro, you can add the SingleStep macro action to your macro at the point where you want single stepping to begin.


2 Answers

If (ActiveWindow.Zoom < 100) Then

    ActiveWindow.Zoom = 100

End If
like image 128
Steven Avatar answered Oct 16 '22 05:10

Steven


Here's a one-liner that will do the same thing:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  ActiveWindow.Zoom = Application.Max(ActiveWindow.Zoom, 100)
End Sub
like image 25
JimmyPena Avatar answered Oct 16 '22 06:10

JimmyPena