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
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

rlv01

Well-known Member
Joined
May 16, 2017
Messages
754
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
 

Zelcombe

New Member
Joined
Apr 3, 2018
Messages
9
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
 

Watch MrExcel Video

Forum statistics

Threads
1,098,990
Messages
5,465,855
Members
406,450
Latest member
gunner12

This Week's Hot Topics

Top