It is possible to give a different textalignement for some columns of a
listbox?
I want that some columns have a textalignement left and some columns have a
textalignement right. I don't know if it is possible. I have to put the
command in a macro.
It is possible to give a different textalignement for some columns of a
listbox?
I want that some columns have a textalignement left and some columns have a
textalignement right. I don't know if it is possible. I have to put the
command in a macro.
Hi R4gtl,
It's not possible without jumping through some hoops. If you use a
fixed-width font like Courier New, then you can pad the columns with leading
spaces to achieve right-alignment.
Otherwise, you could look into using the ListView control if it's available
to you.
--
Regards,
Jake Marx
MS MVP - Excel
www.longhead.com
[please keep replies in the newsgroup - email address unmonitored]
R4gtl wrote:
> It is possible to give a different textalignement for some columns of
> a listbox?
> I want that some columns have a textalignement left and some columns
> have a textalignement right. I don't know if it is possible. I have
> to put the command in a macro.
Hi,
This code should give you an start point. It may need tweaking for
different fonts and possible the scrollbars.
Create a userform and add a listbox, label and 2 commandbuttons.
Paste this code into the userform code module.
It uses the label to approximate the padding required to right align the
text. If you specify column widths then make sure they are all specified.
'-----------------Start Code
Sub AlignListColumn(LBox As MSForms.ListBox, _
WhichColumn As Integer, AlignRight As Boolean)
'
' Align specified column
'
Dim vntColWidths As Variant
Dim sngWidth As Single
Dim strTemp As String
Dim labTester As MSForms.Label
Dim intItem As Integer
If Not AlignRight Then
' Normal left alignment
With LBox
For intItem = 0 To .ListCount - 1
.List(intItem, WhichColumn) = _
Trim(.List(intItem, WhichColumn))
Next
End With
Else
' Right align specified column
If WhichColumn > LBox.ColumnCount Then Exit Sub
Set labTester = Me.Label1
labTester.WordWrap = False
With LBox
If .ColumnWidths <> "" Then
vntColWidths = Split(.ColumnWidths, ";")
' fudge for gap between cols
sngWidth = Val(vntColWidths(1)) - 5
Else
' fudge for gap between cols
sngWidth = (.Width / .ColumnCount) - _
((.ColumnCount - 1) * 5)
End If
If sngWidth <= 0 Then Exit Sub
For intItem = 0 To .ListCount - 1
strTemp = Trim(.List(intItem, WhichColumn))
labTester.AutoSize = False
labTester.Width = .Width
labTester.Caption = strTemp
labTester.AutoSize = True
Do While labTester.Width <= sngWidth
labTester.Caption = " " & labTester.Caption
Loop
.List(intItem, WhichColumn) = labTester.Caption
Next
End With
End If
End Sub
Private Sub CommandButton1_Click()
AlignListColumn ListBox1, 0, True
AlignListColumn ListBox1, 1, False
AlignListColumn ListBox1, 2, True
End Sub
Private Sub CommandButton2_Click()
AlignListColumn ListBox1, 0, False
AlignListColumn ListBox1, 1, True
AlignListColumn ListBox1, 2, False
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "One"
.AddItem "Two"
.AddItem "Three"
.AddItem "Four"
.AddItem "Five"
.List(0, 1) = "One"
.List(1, 1) = "Two"
.List(2, 1) = "Three"
.List(3, 1) = "Four"
.List(4, 1) = "Five"
.List(0, 2) = "One"
.List(1, 2) = "Two"
.List(2, 2) = "Three"
.List(3, 2) = "Four"
.List(4, 2) = "Five"
Label1.Font.Bold = .Font.Bold
Label1.Font.Italic = .Font.Italic
Label1.Font.Name = .Font.Name
Label1.Font.Size = .Font.Size
End With
End Sub
'-----------------End Code
Cheers
Andy
R4gtl wrote:
> It is possible to give a different textalignement for some columns of a
> listbox?
> I want that some columns have a textalignement left and some columns have a
> textalignement right. I don't know if it is possible. I have to put the
> command in a macro.
>
--
Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks