1

When I activate a sheet I want excel to do the following process:

Is cell "A1 and cell "Z1" in the range of my screen?

Yes: Do nothing

No:

Range("A1:Z1").Select
ActiveWindow.Zoom = True

I want this because my file has to open on different screens/screen resolutions and everything has to be visible for everyone.

I hope somebody can help me.

3 Answers3

0

From the link i posted in the comment, i have come up with this code

Sub temp()

    Dim zoomRatio As Integer
    zoomRatio = 100

    Do While (Not (CellIsInVisibleRange(ActiveSheet.Range("Z1"))))
        zoomRatio = zoomRatio - 1
        ActiveWindow.Zoom = zoomRatio
    Loop

End Sub


Function CellIsInVisibleRange(cell As Range)
    CellIsInVisibleRange = Not Intersect(ActiveWindow.VisibleRange, cell) Is Nothing
End Function

Update:

Revised the subroutine to Zoom in if more than column Z is visible and Zoom out if less than Col Z is visible

Sub temp()

    Dim zoomRatio As Integer
    zoomRatio = ActiveWindow.Zoom

    Do While (Not (CellIsInVisibleRange(ActiveSheet.Range("Z1"))))
        zoomRatio = zoomRatio - 2
        ActiveWindow.Zoom = zoomRatio
    Loop

    Do While (CellIsInVisibleRange(ActiveSheet.Range("AA1")))
        zoomRatio = zoomRatio + 2
        ActiveWindow.Zoom = zoomRatio
    Loop

End Sub
nightcrawler23
  • 2,056
  • 1
  • 14
  • 22
0

I fixed it. Thank you!

Private Sub Worksheet_Activate()

If CellIsInVisibleRange(ActiveSheet.Range("Z2")) Then

'Nothing
Else
    Range("A2:Z2").Select
    ActiveWindow.Zoom = True
    Range("D3").Select
End If

End Sub

Function CellIsInVisibleRange(cell As Range)

CellIsInVisibleRange = Not Intersect(ActiveWindow.VisibleRange, cell) Is Nothing

End Function
0

Couldn't you just check if the range already fits and if it doesn't call the zoom method?

Option Explicit

Public Sub RunMe()
    FitColumns "A:Z"
End Sub

Private Sub FitColumns(addr As String)
    Dim oldSel As Range
    Dim colRange As Range

    Set oldSel = Selection
    Set colRange = Range(addr).Resize(1)
    If Intersect(colRange, ActiveWindow.VisibleRange) Is Nothing Then
        colRange.Select
        ActiveWindow.Zoom = True
        oldSel.Select
    End If
End Sub
Ambie
  • 4,872
  • 2
  • 12
  • 26