In Excel 2007, I inserted an ActiveX label onto my worksheet. I right-clicked on it and viewed Properties and managed to change the TextAlign property to 2 (frmTextAlignCenter).
This aligns the label caption's text to the center of the label (horizontally), but the text remains at the TOP of the label. How do I center the caption's text VERTICALLY so that it is in the smack middle of the label?
I've searched "vertical alignment" in SO but nothing comes up for how to do this for an Excel label's caption.
To align text vertically on a page, head over to the “Layout” tab and select the small icon in the bottom-right corner of the “Page Setup” group. This opens the “Page Setup” dialog box. Select the “Layout” tab and then click the arrow next to “Vertical Alignment” in the “Page” section.
There is a trick to do it with a single label. Add an transparent gif image of 1x1 pixel (here) and set the PictureAlignment property to PicturePositionLeftCenter.
There's no way to do it directly. This post has a clever way to accomplish it, though. Make 2 boxes, with the inner one autosized around the text, and position that inner box at the midpoint of the outer box.
I just tried the approach outlined in the top voted answer and it worked perfectly. To add a little to the approach though - if you have many labels for example, I did the following:
Property | Value |
---|---|
Name | GIF |
Picture | (set to be the 1x1 transparent gif picture [link]) |
Visible | False |
Property | Value |
---|---|
Tag | "LabelAlignmentTheme" |
UserForm_Initialize
Private Sub UserForm_Initialize()
'Apply the fix in https://stackoverflow.com/questions/6859127/how-do-i-vertically-center-the-text-in-an-excel-labels-caption
'To all labels with the matching Tag
Dim ctrl As MSForms.control
For Each ctrl In Me.controls
If TypeOf ctrl Is MSForms.label And ctrl.Tag = "LabelAlignmentTheme" Then
Dim label As MSForms.label
Set label = ctrl
Set label.Picture = Me.GIF.Picture 'read the picture from the picture control
label.PicturePosition = fmPicturePositionLeftCenter
End If
Next
End Sub
I like this use of Tag
, it feels like a css style. Obviously you can skip the check for the tag (remove the second half of the And statement) and align absolutely everything but I think this is a more realistic scenario where you only want some aligned.
By storing the image in a shared hidden picture somewhere in the form, it is embedded in the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With