I never noticed this Listbox property before, but it seems like it could be useful. According to help, the TextColumn property sets the column whose value the Text property will return. The BoundColumn property determines what the Value property returns, so this seems to bet the step sister of that.
You can now have easy access to two columns in a multicolumn Listbox. I don’t really see a downside since everything in a Listbox is text anyway, so the difference between Value and Text should be nothing. If you don’t set the TextColumn property, the Text property returns the same as Value.
This example shows two ways to access a different column than the BoundColumn: One using the List property and one using the Text/TextColumn properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Private Sub CommandButton1_Click() Dim sMsg As String With Me.ListBox1 sMsg = "Use TextColumn" & vbNewLine & String(15, "-") & vbNewLine sMsg = sMsg & .Value & vbTab & .Text sMsg = sMsg & vbNewLine & vbNewLine sMsg = sMsg & "No TextColumn" & vbNewLine & String(15, "-") & vbNewLine sMsg = sMsg & .Value & vbTab & .List(.ListIndex, 1) End With MsgBox sMsg End Sub Private Sub UserForm_Initialize() Dim i As Long With Me.ListBox1 .ColumnCount = 2 .BoundColumn = 1 .TextColumn = 2 For i = 1 To 10 .AddItem i .List(.ListCount - 1, 1) = i * 10 Next i End With End Sub |
Hi Dick
A great use for this is having a two-column list box with and ID in the first column set to zero width. Hence the BoundColumn is 1 and the TextColumn is 2.
ListBox.Text is what the user sees (e.g. a name)
ListBox.Value is the corresponding ID.
Regards
Stephen Bullen
That *is* a great use. I’m sure I’m the last guy to discover this. :)
Great post! I wonder… when typing into the combobox, how can I make it to do the matching with a specific column?
It will match on the column you specify as
.TextColumn
. If you don’t specify a.TextColumn
, it matches on the first column.