VBA | With opening/activating sheet > edit viewing screen/zoom

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
Hi all,

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

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

Yes:
Do nothing

No:
Code:
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.


I hope somebody can help me.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Put this in the worksheet object in VBA

Code:
Private Sub Worksheet_Activate()
If ActiveWindow.Zoom > 100 Then ActiveWindow.Zoom = 100
End Sub
 
Upvote 0

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
This code doesn't work for me. What I need, is:
When cell Z1 isn't in my screen/view THEN Range("A1:Z1").Select
ActiveWindow.Zoom = True
 
Upvote 0

tonyyy

Well-known Member
Joined
Jun 24, 2015
Messages
1,784
Office Version
  1. 2010
Platform
  1. Windows
You might consider the following...

Code:
Private Sub Worksheet_Activate()
If Intersect(ActiveWindow.VisibleRange, Range("Z1")) Is Nothing Then
    Range("A1:Z1").Select
    ActiveWindow.Zoom = True
End If
End Sub

Cheers,

tonyyy
 
Upvote 0

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
You might consider the following...

Code:
Private Sub Worksheet_Activate()
If Intersect(ActiveWindow.VisibleRange, Range("Z1")) Is Nothing Then
    Range("A1:Z1").Select
    ActiveWindow.Zoom = True
End If
End Sub

This code always does
Code:
Range("A1:Z1").Select
ActiveWindow.Zoom = True

That code only has to be activated when I activate the sheet and Cell Z1 is not visable in my screen

Thanks for your time
 
Upvote 0

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
I fixed the problem with this code:

Code:
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
 
Upvote 0

Forum statistics

Threads
1,191,121
Messages
5,984,764
Members
439,909
Latest member
daigoku

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
Top