Centring Userform On Screen

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using (trying) this code to ensure a dynically sized userform stays centred on the user's screen without a static screen size being defined. (will centre regardless of the screen size).

For the most part it works pretty slick, expect on my lap top. The userform centres horizontally, but not vertically. The top of userform (with the X) is hidden by the worksheet column header. (so I caan't access the X or move the form should I wish to).

Code:
        frmservice.Top = Application.Top + (Application.UsableHeight / 2) - (frmservice.Height / 2)
        frmservice.Left = Application.Left + (Application.UsableWidth / 2) - (frmservice.Width / 2)

Can anyone suggest any improvements that would eliminate this issue while still serving its intended purpose for the most part?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You don't have to use code to do this... simply select the UserForm in the VBA editor and then set its StartUpPosition to 2-CenterScreen in the Properties window (press F4 if you don't see it).
 
Upvote 0
Solution
Thanks Rick! Will this method maintain the centering as the userform is resized?
 
Upvote 0
When you show the UserForm, no matter what size it is at the time it is shown, that UserForm will be centered. If you mean you are going to change its size after it is on the screen, then no, that resized UserForm will not re-center itself. If it is this latter resizing method you are referring to, then how is it being resized? If you are doing it in code, then all you have to do is move the UserForm half the change in length and half the change in width that you are resizing it by from its current location.
 
Upvote 0
Thanks Rick .... is the code I posted in post #1 not doing this? It works on my desktop, and works horizontally on my laptop. It just doesn't appear to centre it vertically on my laptop. With the taptop, the top portion of the userform is cut off. (too high).
 
Upvote 0
An alternative approach could be to use the following...
VBA Code:
    Dim X  As Long
    Dim Y  As Long
    
    X = GetSystemMetrics(0)
    Y = GetSystemMetrics(1)
'this sets to full screen
    Me.Width = 3 / 4 * X
    Me.Height = 3 / 4 * Y
'or you can fiddle with Me.Top etc and position elsewhere.
'this is not dependent on the position of the excel application window
 
Upvote 0
Thanks diddi for your suggestion. I tried it, but GetSystemMetrics threw me a "Sub or Function Not Defined" error.
 
Upvote 0
did you declare it? it s an API fn

VBA Code:
Public Declare PtrSafe Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
 
Upvote 0
So, I've had a chance to correct and test this and it's not doing as anticipated.
Here is the code that I am testing with that sets the size of the userform based on the value of 'srvsnum', and then is supposed to centre the userform on the screen.

Code:
If srvsnum > 4 Then
        frmservice.Height = 479
        frmservice.Width = 713
    Else
        frmservice.Height = 288
        If srvsnum = 4 Then
            frmservice.Width = 713
        ElseIf srvsnum = 3 Then
            frmservice.Width = 540
        Else
            frmservice.Width = 366
        End If
    End If
    'Stop
    x = GetSystemMetrics(0)
    Y = GetSystemMetrics(1)
    'this sets to full screen
    frmservice.Width = 3 / 4 * x
    frmservice.Height = 3 / 4 * Y
    'frmservice.Top = Application.Top + (Application.UsableHeight / 2) - (frmservice.Height / 2)
    'frmservice.Left = Application.Left + (Application.UsableWidth / 2) - (frmservice.Width / 2)
        /code]

With Didi's suggestion, the height and width of the userform as I had defined was lost, and the resulting userform was much bigger than needed. Although the top left corner of the userform was on the monotor screen, the size of the userform put it off the screen.

The way I had been doing it, although not centring the userform vertically on the monitor (on my laptop only), it did maintain the right size and was horizontally centred.

Perhaps I misunderstood how to applu Didi's suggestion?
 
Upvote 0
VBA Code:
x = GetSystemMetrics(0)
Y = GetSystemMetrics(1)
If srvsnum > 4 Then
        frmservice.Height = 479
        frmservice.Width = 713
    Else
        frmservice.Height = 288
        If srvsnum = 4 Then
            frmservice.Width = 713
        ElseIf srvsnum = 3 Then
            frmservice.Width = 540
        Else
            frmservice.Width = 366
        End If
    End If
    
    frmservice.Top = (3 / 4 * Y - frmservice.Height)/2 ' this halves the remaining vertical space not covered by window
    frmservice.Left = (3 / 4 * X - frmservice.Width)/2 ' this halves the remaining horizontal space not covered by window

hope this gets you moving in the right direction
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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