Excel Window Autozoom based on cell

pingme89

Board Regular
Joined
Jan 23, 2014
Messages
172
Office Version
  1. 365
Platform
  1. Windows
I have an Excel application where I only want a specified range visible. Whenever a user moves Excel to a new window with a different resolution, the zoom factor gets messed up. Also users do not necessarily keep the Excel app in full screen either.

So I have a list of Worksheet Names and Cell Addresses on a worksheet labelled "Settings".

For Example

Sheet1AJ48
Sheet2AC59
Sheet3Y36
Sheet4AT123

I want to loop through the Sheet names and starting from 200 zoom factor to reduce the zoom factor by 1 until the specified Cell is visible in the Excel window.

The code I have doesn't work.

VBA Code:
Sub EnsureCellVisible()
    Dim wsSettings As Worksheet
    Dim ws As Worksheet
    Dim cell As Range
    Dim Marker As Variant
    Dim zoomFactor As Double
    Dim CellRow As Long
    Dim CellMark As String
    
    ' Set the worksheet containing the settings
    Set wsSettings = ThisWorkbook.Sheets("Settings")

    ' Loop through each row in the Settings worksheet starting from row 2
    For Each cell In wsSettings.Range("A2:A" & wsSettings.Cells(wsSettings.Rows.Count, "A").End(xlUp).Row)
        ' Get the worksheet name from column A
        Set ws = ThisWorkbook.Sheets(cell.Value)
        CellRow = cell.Row
        ' Get the cell address from column B
        Marker = ThisWorkbook.Worksheets("Settings").Range("B" & CellRow).Text
        ' Activate the worksheet
        ws.Range("A1").Activate
        
        For i = 200 To 10 Step -1
            ws.Parent.Windows(1).Zoom = i
            If ws.Range(Marker).Visible Then
                Exit For
            End If
        Next i
    Next cell
End Sub


The portion of the code that doesn't seem to work is this: If ws.Range(Marker).Visible Then
I almost feel that the cell is "visible" meaning it the row or column it is in, is visible.
I want to check if it is optically visible in the window.

Any help would be greatly appreciated.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
See if this works for you.

VBA Code:
Sub EnsureCellVisible()
    Dim wsSettings As Worksheet
    Dim ws As Worksheet
    Dim cell As Range, Visible As Range
    Dim Marker As Variant
    Dim zoomFactor As Double
    Dim CellRow As Long
    Dim CellMark As String
    
    ' Set the worksheet containing the settings
    Set wsSettings = ThisWorkbook.Sheets("Settings")

    ' Loop through each row in the Settings worksheet starting from row 2
    For Each cell In wsSettings.Range("A2:A" & wsSettings.Cells(wsSettings.Rows.Count, "A").End(xlUp).Row)
        ' Get the worksheet name from column A
        Set ws = ThisWorkbook.Sheets(cell.Value)
        CellRow = cell.Row
        ' Get the cell address from column B
        Marker = wsSettings.Range("B" & CellRow).Text
        ' Activate the worksheet
        ws.Activate
        Set Visible = Range(Marker)
        
        For i = 200 To 10 Step -1
            ws.Parent.Windows(1).Zoom = i
            If CellIsVisible(Visible) Then
                Exit For
            End If
        Next i
    Next cell
End Sub
VBA Code:
Function CellIsVisible(cell As Range) As Boolean
    CellIsVisible = Not Intersect(ActiveWindow.VisibleRange, cell) Is Nothing
End Function
 
Upvote 0
Solution
Thanks Murray.
I have modified the code slightly as I also needed Range("A1") to also be visible.

Here is the completed code:
VBA Code:
Sub EnsureCellVisible()
    Dim wsSettings As Worksheet
    Dim ws As Worksheet
    Dim cell As Range
    Dim Marker As Variant
    Dim CellRow As Long
    Dim CellMark As String
    Dim Visible As Range
    
    ' Set the worksheet containing the settings
    Set wsSettings = ThisWorkbook.Sheets("Settings")

    ' Loop through each row in the Settings worksheet starting from row 2
    For Each cell In wsSettings.Range("A2:A" & wsSettings.Cells(wsSettings.Rows.Count, "A").End(xlUp).Row)
        ' Get the worksheet name from column A
        Set ws = ThisWorkbook.Sheets(cell.Value)
        CellRow = cell.Row
        ' Get the cell address from column B
        Marker = ThisWorkbook.Worksheets("Settings").Range("B" & CellRow).Text
        ' Activate the worksheet
        ws.Activate
        ws.Range("A1").Select
        Set Visible = Range(Marker)
        
        For i = 200 To 10 Step -1
            ws.Parent.Windows(1).Zoom = i
            If CellIsVisible(Visible) Then
                
                Exit For
            End If
        Next i
    Next cell
End Sub

Function CellIsVisible(cell As Range) As Boolean
    CellIsVisible = Not Intersect(ActiveWindow.VisibleRange, cell) Is Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,216,169
Messages
6,129,272
Members
449,497
Latest member
The Wamp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top