Userform object will not center on maximize

oblix

Board Regular
Joined
Mar 29, 2017
Messages
183
Office Version
  1. 2010
Platform
  1. Windows
Hi there
I am trying to make userform full screen size and center the object once maximized.
I have achieved the full screen part, but my code does not send object to center of screen, but top left instead.

My code is as follows:

Private Sub UserForm_Activate()

Me.Height = Application.Height
Me.Width = Application.Width
Me.Left = Application.Left
Me.Top = Application.Top

Me.MultiPage1.Left = (Me.MultiPage1.Width / 2) - (Me.MultiPage1.Width / 2)
Me.MultiPage1.Top = (Me.MultiPage1.Height / 2) - (Me.MultiPage1.Height / 2)

End

Please help!
Thank you
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I don't have time to create your form from scratch for testing but try this:

Rich (BB code):
Private Sub UserForm_Activate()

   Me.Height = Application.Height
   Me.Width = Application.Width
   Me.Left = Application.Left
   Me.Top = Application.Top

   Me.MultiPage1.Left = Me.Width / 2 - Me.MultiPage1.Width / 2
   Me.MultiPage1.Top = Me.Height / 2 - Me.MultiPage1.Height / 2

End
 
Upvote 0
Please have a read of this link, if your code was any longer I would not have replied, unformatted code can be difficult to read. You will see from the link that formatting your code correctly is very simple and makes it easier for people helping you. I believe that using tags is only a suggestion here but many other forums insist on it as a rule and some are very strict with it.


To answer your question, the first part of the calculation needs to refer to the form width, not the object width.

VBA Code:
Me.MultiPage1.Left = (Me.Width / 2) - (Me.MultiPage1.Width / 2)
Me.MultiPage1.Top = (Me.Height / 2) - (Me.MultiPage1.Height / 2)
 
Upvote 0
Solution
Sorry for pasting wrongly and will format the code according to link.
It's not wrong (or at least not on this forum) it just makes it stand out from the rest of your post so that it's easier to read. Sometimes the moderators will edit your post and add the tags if they see unformatted code, but I've never known them to take any action against anyone not doing it (unlike a certain other forum that both Jeff and myself are familiar with).
 
Upvote 0
The rules of this forum do not require code tags but here is why they are a good idea:

  • The spacing in your code is preserved in code tags. Otherwise leading spaces on a line are removed for display.
  • It is presented in a monospaced font, which makes it more readable since spacing is often deliberate in code
  • Keywords are color-coded
  • Long code appears in a scroll box so it does not clutter up the page
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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