Centring Userform On Screen

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,868
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

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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,970
Office Version
  1. 2016
Platform
  1. Windows
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).
 
Solution

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,868
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thanks Rick! Will this method maintain the centering as the userform is resized?
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,970
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,868
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

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).
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,297
Office Version
  1. 2010
Platform
  1. Windows
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
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,868
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thanks diddi for your suggestion. I tried it, but GetSystemMetrics threw me a "Sub or Function Not Defined" error.
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,297
Office Version
  1. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,396
Messages
5,636,059
Members
416,896
Latest member
Hozier

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