Caption on userform centered?

Cowboy Henk

Board Regular
Joined
Jul 5, 2005
Messages
129
Howdy,

Is there a possibility in VBA to always center the caption of a userform. I tried doing it with spaces, but this didn't work on all computers(depends on screen resolution probably). How can I make this work?
Thanks for your help.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

Heres my logic.

Take forms width & divide by 2 to give horiztonal centre point. Do the same for height.

Now you get the co-ordinates for for the centre point in the form.

Next move the label (you used the word caption which is a property of a label) to the centre point.

So using the forms on Initialise (NB my form was called UserForm1 and label called Label1 - change where required)

Code:
Private Sub UserForm_Initialize()

Dim fWidth As Integer, fHeight As Integer, lWidth As Integer, lHeight As Integer

UserForm1.Label1.AutoSize = True

fWidth = UserForm1.InsideWidth / 2

fHeight = UserForm1.InsideHeight / 2

lWidth = UserForm1.Label1.Width / 2

lHeight = UserForm1.Label1.Height / 2

UserForm1.Label1.Left = fWidth - lWidth

UserForm1.Label1.Top = fHeight - lHeight

End Sub

A nice simple code.

Hope it helps
 
Upvote 0

Forum statistics

Threads
1,224,053
Messages
6,176,102
Members
452,706
Latest member
must_try_harder

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