Center a Label in a userform?

NewYears1978

Board Regular
Joined
Feb 25, 2014
Messages
107
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.)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,
 
Upvote 0
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!
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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