Adjust magnification without selection in Excel VBA

Is it possible to set the zoom level based on screen resolution without Select?

I followed the code:

Sheets(1).Range("A1:AC1").Select
ActiveWindow.Zoom = True

taken from https://stackoverflow.com>

The desired code would look something like this:

Range("A1:AC1").Width.Zoom=True

Refresh. Why do I want to avoid the choice?

  • My sheet has hidden columns based on user preferences. Therefore, some columns in the range A1: AC1 are hidden. I cannot select a single column because this particular column may be hidden.
  • The selection raises events. Of course, I can disable events, but disabling events has some side effects that I want to avoid.
+4
source share
3

. . , . , "BoxForZoom". . :

Sheet1.Shapes("BoxForZoom").Visible = True
Sheet1.Shapes("BoxForZoom").Select
ActiveWindow.Zoom = True
Sheet1.Shapes("BoxForZoom").Visible = False
0

, .

, AC ( 29) , Zoom= 100 , 29 + 1 VisibleRange:

Sub ShrinkWindow()
    Dim i As Long, r As Range

    For i = 100 To 1 Step -1
        ActiveWindow.Zoom = i
        Set r = ActiveWindow.VisibleRange
        If r.Columns.Count = 29 + 1 Then Exit Sub
    Next i
End Sub
+2

. . . , .

Private Sub ZoomToRange(target As Range)
    'Get the window from the target range.
    Dim wnd As Window
    Set wnd = ActiveWindow

    'Find out what you need to scale to.
    Dim scaling As Long
    scaling = 100 * wnd.Width / target.Width
    'Limit to max and min zoom level.

    If scaling > 400 Then
        wnd.Zoom = 400
    ElseIf scaling < 10 Then
        wnd.Zoom = 10
    Else
        wnd.Zoom = scaling
    End If
    'Scroll to the upper left cell
    target.Cells(1, 1).Activate
End Sub
+2

Source: https://habr.com/ru/post/1653688/


All Articles