Center a Label in a userform?

NewYears1978

Board Regular
Joined
Feb 25, 2014
Messages
105
I feel really dumb, but I know you can't do this with properties alone.

I am using a userform that fits the screen (fullscreen on any resolution) and I need to be able to center some labels on the form but I can't find anything via google about this.

Thanks in advance.

(Alternatively I could use an image instead of Label, but images have no text label so then thet presents a new problem.)
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Welcome to the Board!

It's under TextAlign in the Label's properties. If you mean that you need to dynamically align them across the form no matter the size, then you'd need to capture the form/screen size and adjust the Length accordingly as well.

Can you post the code you're using to size the form?

HTH,
 

NewYears1978

Board Regular
Joined
Feb 25, 2014
Messages
105
Hey there. Yes I mean that I need the lable itself to be centered, not just the text.

I am new to VBA and trying to learn as I go in making a companion app for a game I play so a lot of my code is bits and pieces and probably wrong.

I am wanting simple buttons that are just black boxes with orange borders, I started with nomal buttons, but then I found you can't modify the borders (to remove 3d look) so then I tried images, which if I want to make full custom images would work but is not ideal, and default images have no text label. So then I tried label, which works fine except that there is no center position for them.

The code, which I found onlnie (and probably not the right code to use but it is just what I am working with now) is

Code:
Private Sub UserForm_Activate()
With Application
    Me.Top = .Top
    Me.Left = .Left
    Me.Height = .Height
    Me.Width = .Width
End With
End Sub
Eventually this app will run fullscreen on any resolution and be transparent. In theory. Hard to explain without explaining exactly what I am trying to write!
 

NewYears1978

Board Regular
Joined
Feb 25, 2014
Messages
105
I'd probably just add:

Me.Label1.Width = Application.Width

Won't that make the "button" label the entire width of the screen? Then it will look long a really long button rather than a small button in the center of the screen?

Maybe I should worry about positioning later after I get most of the other stuff down..I really have no clue what I am doing just trying to learn ;)


Is there a value for setting the position? Then I could use something like the application width divided by 2, then minus the button's width?
 
Last edited:

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
Try:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Activate()<br>  <SPAN style="color:#00007F">With</SPAN> Application<br>    Me.Top = .Top<br>    Me.Left = .Left<br>    Me.Height = .Height<br>    Me.Width = .Width<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>  Me.Label1.Left = (Me.Width / 2) - (Me.Label1.Width / 2)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

(I think you want to minus half the label's width)

Hope thta helps,

Mark
 

NewYears1978

Board Regular
Joined
Feb 25, 2014
Messages
105
Try:

Private Sub UserForm_Activate()
With Application
Me.Top = .Top
Me.Left = .Left
Me.Height = .Height
Me.Width = .Width
End With
Me.Label1.Left = (Me.Width / 2) - (Me.Label1.Width / 2)
End Sub


(I think you want to minus half the label's width)

Hope thta helps,

Mark
Yes this worked, thanks kindly. Was similar to what I have posted above :) I knew something like this had to work because it is similar to what I used to do in HTML/CSS
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
Yes this worked, thanks kindly. Was similar to what I have posted above :) ...

Glad that worked of course and of course I just swiped your code and tacked in that last line.

...to what I used to do in HTML/CSS
Scary stuff that. I've only written maybe one help file (.chm) and HTML seems just a bit tough/slow to get. My poor brain can barely remember what I'm supposed to be doing with intellisense...

Mark
 

Watch MrExcel Video

Forum statistics

Threads
1,098,992
Messages
5,465,867
Members
406,452
Latest member
GroupGoal

This Week's Hot Topics

Top