VBA text alignment in text box

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all,

How to set alignment for part of the text in textbox to the left? This is part of code I am using (tb6 is textbox name). I need part of text after Chr(13) to be alligned to the left (so
VBA Code:
"This field displays the current mode and status the machine"
should be centered, but
VBA Code:
                                                & Chr(13) & Chr(13) & "Mode:" & "                                                  " _
                                                & "Status:"
should be aligned to the left.

VBA Code:
        If s.EN.Value = True Then
                With tb6.TextFrame
                    .Characters.Text = "This field displays the current mode and status the machine" _
                                                & Chr(13) & Chr(13) & "Mode:" & "                                                  " _
                                                & "Status:"
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlTop
                End With
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about to add some spaces before the first sentence, and all aligned to the left ?
VBA Code:
If s.EN.Value = True Then
                With tb6.TextFrame
                    .Characters.Text = "                      This field displays the current mode and status the machine" _
                                                & Chr(13) & Chr(13) & "Mode:" & "                                                  " _
                                                & "Status:"
                    .HorizontalAlignment = xlHAlignLeft
                    .VerticalAlignment = xlTop
                End With
 
Upvote 0
Solution
Try this.
HTH.
VBA Code:
If s.EN.Value = True Then
        With tb6.TextFrame
            .Characters.Text = "This field displays the current mode and status the machine" _
                                        & Chr(13) & Chr(13) & "Mode:" & "                                                  " _
                                        & "Status:"
            .VerticalAlignment = xlTop
        End With
        tb6.TextFrame2.TextRange.Paragraphs(1).ParagraphFormat.Alignment = msoAlignCenter
        tb6.TextFrame2.TextRange.Paragraphs(3).ParagraphFormat.Alignment = msoAlignLeft
 
Upvote 0
Try this.
HTH.
VBA Code:
If s.EN.Value = True Then
        With tb6.TextFrame
            .Characters.Text = "This field displays the current mode and status the machine" _
                                        & Chr(13) & Chr(13) & "Mode:" & "                                                  " _
                                        & "Status:"
            .VerticalAlignment = xlTop
        End With
        tb6.TextFrame2.TextRange.Paragraphs(1).ParagraphFormat.Alignment = msoAlignCenter
        tb6.TextFrame2.TextRange.Paragraphs(3).ParagraphFormat.Alignment = msoAlignLeft

This one somehow doesn't work for me. Default alignment of textbox is in the center and it displays paragraph(3) in center as well. But the solution with spaces is simple and works for me, so thank you!
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,952
Members
449,198
Latest member
MhammadishaqKhan

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