This page describes two VBA procedures that you can use to
zoom in on a specific range of a worksheet, or to center the screen on a
specific cell. These procedures are intended to be called by other VBA
procedures -- they don't provide much functionality as stand-alone
procedures.
Zooming On A Range
This procedure will zoom the screen so that a specified
range will fill the entire screen. Before using this procedure, there are
two considerations that you need to remember:
The maximum zoom level in Excel if 400%. Therefore,
if the specified range is too small, the worksheet is zoomed to the maximum
level.
You cannot zoom in on an arbitrary range. The Zoom
area must fill the entire screen. Therefore, either the number of rows or
the number of columns must be adjusted so that the zoomed area will fill the
entire screen. The PreserveRows parameter to the ZoomToRange procedure
controls whether the number of rows if fixed (and the number of columns is
adjusted) or the number of columns is fixed (and the number of rows is
adjusted).
Sub ZoomToRange(ByVal ZoomThisRange As Range,
_
ByVal PreserveRows As Boolean)
Dim Wind As Window
Set Wind = ActiveWindow
Application.ScreenUpdating = False
'
' Put the upper left cell of the range in the top-left of the screen.
'
Application.Goto ZoomThisRange(1, 1), True
With ZoomThisRange
If PreserveRows = True Then
.Resize(.Rows.Count, 1).Select
Else
.Resize(1, .Columns.Count).Select
End If
End With
With Wind
.Zoom = True
.VisibleRange(1, 1).Select
End With
End Sub
You can call this procedure from another procedure to
zoom in on a specific range. For example, to zoom in on cells F20:K40, and
allow Excel to adjust the number of columns as required, use
ZoomToRange
ZoomThisRange:=Range("F20:K40"), PreserveRows:=True
|