VBA TextAlign Property of ListBox ActiveX Control in Excel to sets an integer value(1 or 2 or 3) It specifies how text is aligned in a listbox control.
ListBox TextAlign Property – Syntax
Please find the below syntax of ListBox Text Align Property in Excel VBA.
ListboxName.TextAlign=1 – frmTextAlignLeft
Or
ListboxName.TextAlign=2 – frmTextAlignCenter
Or
ListboxName.TextAlign=3 – frmTextAlignRight
Where ListboxName represents the ListBox object. In the above syntax we are using a ‘TextAlign’ property of ListBox object to align listbox items.
ListBox TextAlign Property – Explanation & Example
Here is the example for ListBox Text Align Property. It will take you through how to align Text Align property of list box using Excel VBA. Here you can find or see how we are enable or disable Text Align of list box manually or using code.
ListBox TextAlign Property: Change Manually
Please find the following details how we are changing manually Text Align of listbox property.
-
- Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
- Go To Insert Menu, Click UserForm. Please find the screenshot for the same.
-
- Drag a Listbox on the Userform from the Toolbox. Please find the below screen shot for your reference.
-
- Right click on the List box. Click on properties from the available list.
-
- Now you can find the properties window of listbox on the screen. Please find the screenshot for the same.
-
- On the left side find ‘TextAlign’ property from the available List Box properties.
- On the right side you can find the list of available choices. You can choose one of the following. Please find the below screen shot for your reference.
a. 1 – frmTextAlignLeft
b. 2 – frmTextAlignCenter
c. 3 – frmTextAlignRight
ListBox TextAlign Property:Change Using Code
Please find the following details how we are changing Text Align of listbox property with using Excel VBA code.
-
-
- Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
- Go To Insert Menu, Click UserForm. Please find the screenshot for the same.
-
-
-
- Drag a Listbox on the Userform from the Toolbox. Please find the screenshot for the same.
-
-
-
- Double Click on the UserForm, and select the Userform event as shown in the below screen shot.
-
-
-
- Now can see the following code in the module.
-
Private Sub UserForm_Initialize() End Sub
-
-
- Now, add the following example code1 or code2 OR code3 to the in between above event procedure.
-
Example Code1:
'TextAlign Property of ListBox Control Private Sub UserForm_Initialize() With UserForm1.ListBox1 'ListBox Source Data .RowSource = "A2:B10" 'The below statement will show header in each column .ColumnHeads = True 'The following statement represents number of columns .ColumnCount = 2 'ListBox text items appear left side .TextAlign = 1 End With End Sub
-
-
- Now, Press ‘F5’ to see the following Output.
-
Output: If TextAlign =1 – frmTextAlignLeft
Please find the below output when we set Text Align property value is ‘1’. It is shown in the following Screen Shot.
Example Code2:
'TextAlign Property of ListBox Control Private Sub UserForm_Initialize() With UserForm1.ListBox1 'ListBox Source Data .RowSource = "A2:B10" 'The below statement will show header in each column .ColumnHeads = True 'The following statement represents number of columns .ColumnCount = 2 'ListBox text items appear left side .TextAlign = 2 End With End Sub
-
-
- Now, Press ‘F5’ to see the following Output.
-
Output: If TextAlign =2 – frmTextAlignCenter
Please find the below output when we set Text Align property value is ‘2’. It is shown in the following Screen Shot.
Example Code3:
'TextAlign Property of ListBox Control Private Sub UserForm_Initialize() With UserForm1.ListBox1 'ListBox Source Data .RowSource = "A2:B10" 'The below statement will show header in each column .ColumnHeads = True 'The following statement represents number of columns .ColumnCount = 2 'ListBox text items appear left side .TextAlign = 3 End With End Sub
-
-
- Now, Press ‘F5’ to see the following Output.
-
Output: If TextAlign =3 – frmTextAlignRight
Please find the below output when we set Text Align property value is ‘3’. It is shown in the following Screen Shot.
Can we select which column in a list box is to be aligned; e.g. we need text to be aligned left or centre but numbers should be aligned right.