Selected Answer
Hi Marko,
You might want to try the following. In a separate regular code module copy the following:
Declare Function GetSystemMetrics32 Lib "User32" _
Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
In another module copy the following code and run it with the file open to get the Width and Height of the file when open on your machine.
Sub App_Width_Height()
' macro written by Willied24
Dim AppW As Long
Dim AppH As Long
Dim AppTop As Long ' distance from top of screen
Dim AppLeft As Long ' distance from left edge of screen
AppW = Application.Width
AppH = Application.Height
AppTop = Application.Top
AppLeft = Application.Left
MsgBox AppW & " wide x " & AppH & " high"
MsgBox "Top = " & AppTop & " Left = " & AppLeft
End Sub
Then in the "ThisWorkbook" code window, at the top left select "Workbook". It will default with Workbook_Open(). Copy and paste the following:
' macro written by Willied24 (Mar-2023) for teachexcel.com
Dim wM As Long, hM As Long ' screen resolution of my machine
Dim wY As Long, hY As Long ' screen resolution of user's machine
Dim srcW As Long, srcH As Long ' width and height of Excel window on the source (your) machine
Dim openW As Long, openH As Long ' width and height values to open window at
'
' edit the following two lines to match your screen resolution size
wM = 1366
hM = 768
'
' get user's screen resolution sizes
wY = GetSystemMetrics32(0) ' width (in pixels)
hY = GetSystemMetrics32(1) ' height (in pixels)
' these values were used for testing purposes
'wY = 1000
'hY = 500
' edit the following to match the file's W & H sizes when open on your machine
srcW = 1030
srcH = 582
' calculate width and height values
openW = srcW * (wY / wM)
openH = srcH * (hY / hM)
'
'' set application sizes to open at
With ActiveWindow
.WindowState = xlNormal
' distance from top and left of screen
.Top = 1
.Left = 1
' width and height of application window
.Width = openW
.Height = openH
End With
It compares the screen resolution of the users machine to that of yours (which is hard coded in the code - edit to your setting). Then some math is done to set the width and height to open. (example file with code also attached)
Hope this helps; if so, please mark my answer as selected.
Cheers :-)