Saturday, December 10, 2016

VBA code to set zoom level for all sheets in workbook!!!

Zoom level in excel is one of the important aspect when we share it with end users. Sometimes, it is mandatory to have specific zool level set to each sheet in workbook. As we know, we can adjust zoom of a single worksheet at a time. So, it become a monotonous job when there are hundreds or thousands of sheets include in a workbook. Someone will be tired to adjust zoom level of numerous sheet in the workbook. However, using excel macro we can set specific zoom level to all sheets in the workbook. it can be done within a minute as soon as macro executed.




Here is a code which make set zoom job very simple:

Sub Set_Custom_Zoom()
' Author: Dreams24
' Written for VBA Tricks and tips blog
' https://www.vbatricksntips.com

'Declare variables
Dim zoomVal As Integer
Dim sht As Worksheet

' Code to handle runtime error
On Error GoTo errHandlar:

Application.ScreenUpdating = False

' Assign zoom value to variable
zoomVal = Application.InputBox("Set your custom zoom value to all sheets", "Custom Zoom", 100, Type:=1)

'Loop to each worksheet in active workbook
For Each sht In ActiveWorkbook.Sheets
    sht.Activate
    ActiveWindow.Zoom = zoomVal
Next sht

ActiveWorkbook.Sheets(1).Activate
Application.ScreenUpdating = True

MsgBox "All worksheet set to zoom level: " & zoomVal & "%", vbInformation

'Error handling code for runtime errors
errHandlar:

If Err.Number > 0 Then
MsgBox "An error has occured. See below error desciption for details." & vbNewLine & vbNewLine & "VBA Error No: " & Err.Number & vbNewLine & "VBA Error Description: " & Err.Description
End If

End Sub

No comments: