VBA to center Label vertically with the center of a userform that is not full screen

Zelcombe

New Member
Joined
Apr 3, 2018
Messages
9
Hello Everyone,

Im using Excel 2013 and am trying to center a label in a UserForm, and keep it centered even if the width of the userform changes.

I manipulated the example from this thread https://www.mrexcel.com/forum/excel-questions/837057-center-label-userform.html, but with no luck. Any help would be greatly appreciated.

Code:
Private Sub UserForm_Activate()
    With LendStart
        .Height = 177
        .Width = 642
        .StartUpPosition = 0
        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
        .Caption = "Get Started"
    End With
    Select Case Time
        Case Is < 0.5
            With LendStart.Label1
                .AutoSize = False
                .Font.Size = 12
                .Font.Bold = True
                .Caption = "Good Morning " & Split(Application.UserName)(0) & "!"
                .TextAlign = fmTextAlignCenter
                .Left = LendStart.Left + (0.5 * LendStart.Width) - (0.5 * LendStart.Width)
                .AutoSize = True
            End With
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this
Code:
Private Sub UserForm_Activate()
    With LendStart
        .Height = 177
        .Width = 642
        .Left = 0.5 * Me.Width - (0.5 * .Width)
        .Top = 0.5 * Me.Height - (0.5 * .Height)
        .Caption = "Get Started"
    End With
 
Upvote 0
Thanks rlv01!

I should have worded this better, my apologies. I was looking to center LendStart.Label1 in the UF, so what i did is applied your suggestion to the Case and it helped alot (see code below). The Label still isnt quite centered when the userform changes width, but maybe its just a matter of doing a small tweak to the code.

Code:
            With LendStart.Label1
                .AutoSize = False
                .Font.Size = 12
                .Font.Bold = True
                .Caption = "Good Morning " & Split(Application.UserName)(0) & "!"
                .TextAlign = fmTextAlignCenter
                .Left = 0.5 * Me.Width - (0.5 * .Width)
                .AutoSize = True
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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