UserForm left and top - how to understand?

vbaResearch

New Member
Joined
Feb 4, 2022
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hello forum,
my display is 1980x1080
When I set UserForm1.Left = 0 it goes fine to the left of the screen, but... what is going next I do not understand...

The center of the screen should be ".Left = 960"
But when I set it to 960, the userform appears about 200 or 300 pixels further from the display center.

Then I found out that maximum value of left property which provides at least left edge visible on the screen is 1470. Why 1470???

My display is 1980 pixels wide, but form hides from the screen when value is greater than 1470. (As if there was a problem with scaling or something, but what if I open it on another PC with another display size)

So the question is why userforfm hides when left is more than 1470 but not 1980?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I don't ask "Why?", but use .Top, .Left, .Width and .Height to get the dimension of the working window:
VBA Code:
Debug.Print "L:", Application.Left
Debug.Print "T:", Application.Top
Debug.Print "W:", Application.Width
Debug.Print "H:", Application.Height

Then you can play with userform position and size using its .Top, .Left and its .Zoom
 
Upvote 0
I've found that placement and sizing code works weirdly when it is in the Initialize event, but works great in the Activate event.
 
Upvote 0
It's about scaling screen and userform width.
The screen have pixels as unit, userform have points as unit. Relation between them is 3/4.
Also, if you need do find center, move userform to the left for half of userform width.
Here is example.
VBA Code:
Private Sub UserForm_Initialize()
   
   Dim vResolutionX As Integer, vUserFormWidth As Double, vCenter As Double
   
   StartUpPosition = 0
   vResolutionX = 1980
   vUserFormWidth = Width
   vCenter = vResolutionX * 0.75 / 2 - vUserFormWidth / 2
   Left = vCenter
   
End Sub
 
Upvote 0
Solution
I don't ask "Why?", but use .Top, .Left, .Width and .Height to get the dimension of the working window:
VBA Code:
Debug.Print "L:", Application.Left
Debug.Print "T:", Application.Top
Debug.Print "W:", Application.Width
Debug.Print "H:", Application.Height

Then you can play with userform position and size using its .Top, .Left and its .Zoom
Thank you, for your answer, it is interesting, but it is about Excel application window, not about userform, the userform can be outside of application window,

I've found that placement and sizing code works weirdly when it is in the Initialize event, but works great in the Activate event.
Thank you, for your answer, but after I got understanding about points, I could position my userform as I wanted in initialize event too

It's about scaling screen and userform width.
The screen have pixels as unit, userform have points as unit. Relation between them is 3/4.
Also, if you need do find center, move userform to the left for half of userform width.
Here is example.
VBA Code:
Private Sub UserForm_Initialize()
  
   Dim vResolutionX As Integer, vUserFormWidth As Double, vCenter As Double
  
   StartUpPosition = 0
   vResolutionX = 1980
   vUserFormWidth = Width
   vCenter = vResolutionX * 0.75 / 2 - vUserFormWidth / 2
   Left = vCenter
  
End Sub
Thank you for your answer, yeah, now I get it, the points... now I get an exact result (checked with a ruler :biggrin:). Ough, using points is weird... so when positioning labels, they also use points? it is weird... ookay... Thank you, solved
 
Upvote 0
Thanks for feedback.
I'm glad you see the difference.
 
Upvote 0
@vbaResearch
All what you have done is correct.
I meant that using Application.Top/Left/Width/Height you can get information about the available window.

For example I overlap Excel with the userform using the following code:
VBA Code:
Private Sub UserForm_Activate()
Dim iW As Single, iH As Single, rZoom As Single
iW = Me.Width
iH = Me.Height
Me.Left = Application.Left
Me.Width = Application.Width
Me.Top = Application.Top
Me.Height = Application.Height
'
If Me.Width / iW < Me.Height / iH Then
    rZoom = Me.Width / iW
Else
    rZoom = Me.Height / iH
End If
Me.Zoom = rZoom * 100
End Sub
And this works whichever is the screen resolution, either with normal or maximized window.

And with this other code I move the userform at the far right of the Excel window:
VBA Code:
Private Sub UserForm_Click()
Me.Left = Application.Width + Application.Left - 20
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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