HELP - Adjusting Worksheet Viewable Size to fit Smaller Monitors

lnagel

Board Regular
Joined
May 28, 2018
Messages
117
On my user's screen I want to show the (A1:V37) range of cells in Worksheet1

The Width of (A1:V37) is 977 pixels
The Height of (A1:V37) is 642 pixels

If the users monitor size => 977X642 then everything works perfect - Scaling is 100% - (A1:V37) is centered on screen and offsets (Top:Left) are exactly what they need to be

The issue comes in when trying to scale these parameters to fit to a monitor size that is not Wide enough (977) or High enough (642)

Been struggling with this piece of code for a couple of days now - Any assistance would be greatly appreciated

Code:
Sub Window_Mid()
Dim shW#, maxW#, shH#, maxH#
With Application
    .WindowState = xlMaximized
     maxW = Application.Width 'Returns Monitor Width
     maxH = Application.Height 'Returns Monitor Height
     .WindowState = xlNormal
If maxW >= 977 And maxH >= 642 Then 'If Screen is larger than 977 x 642 then life is good
     Application.Width = 977
     shW = Application.Width
     Application.Height = 642
     shH = Application.Height
Else
     ' Else set the width of Worksheet 1 to 945 x 431
     ' This is hard code for testing on my Laptop Screen - I know this fits
     ' if I can get this part working then I can adapt to other screen width
     Application.Width = 945 '
     shW = Application.Width
     Application.Height = 431
     shH = Application.Height
End If
    .Width = Application.Width
    .Height = Application.Height
    .Top = (maxH - shH) / 2
    .Left = (maxW - shW) / 2
    ' Show me the calculation for offset from Top and offset from Left
    ' Offset works perfect when monitor is larger than desired view
    MsgBox "Offset for Top Margin = " & .Top & " : " & maxH & " - " & shH & " / 2" & vbCr & vbCr & _
    "Offset for Left Margin = " & .Left & " : " & maxW & " - " & shW & " / 2"
End With
' These are the cells that I want to see in the worksheet - and only these
Sheet1.Range("A1:V37").Select
' My attempt at scaling the worksheet screen size to fit monitor
' Works with large monitor - not with small monitor
ActiveWindow.Zoom = True
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this :
Code:
Sub Window_Mid()
Dim shW#, maxW#, shH#, maxH#
With Application
    .WindowState = xlMaximized
     maxW = .Width 'Returns Monitor Width
     maxH = .Height 'Returns Monitor Height
     .WindowState = xlNormal
    If maxW >= 977 And maxH >= 642 Then
         shW = 977
         shH = 642
    Else
         shW = maxW * 0.98
         shH = maxH * 0.98
    End If
    .Width = shW
    .Height = shH
    .Top = (maxH - shH) / 2
    .Left = (maxW - shW) / 2
    MsgBox "Offset for Top Margin = " & .Top & " : " & maxH & " - " & shH & " / 2" & vbCr & vbCr & _
    "Offset for Left Margin = " & .Left & " : " & maxW & " - " & shW & " / 2"
End With
Range("A1:V37").Select
ActiveWindow.Zoom = True
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
End Sub

For the Zoom bit of the code, "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."
Have a look here : http://www.cpearson.com/excel/zoom.htm
 
Upvote 0
Thanks Footoo - Pointed me in the right direction -

Kept having problems with the vba math calculation (shW = maxW * .9 & shH = maxH * .9) not coming out correctly

manually adjusted screensize to 90% and added a sub to tell me what those dimensions actually were

hardcoded those values into Else and "boom" - worked perfect

Couple of thoughts :

1.) Actual values came out for 90% screen at 884.25 X 586.5 - which I'm assuming will disqualify users with monitor resolution of 800 X 600 and Smaller than 90% shows cell Text Strings bleeding over into neighboring cells (font size must not scale) -so not sure how I'm going to handle that (maybe not handle - just pop up MSGBox "Get A Real Monitor or Video Card?) :)

2.) None of this works without the Range(A1:V37) Select statement - after your post I wasn't sure what that statement did - clear to me now

Anyway - Thanks Very Much for the assist
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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