I have a listbox with multiselect options. I populate it using the addItem
function.
I can't find any article about this on Google, but I need to distinguish from the text displayed in the listbox and a real value.
For example:
shown hiddenvalue
-------- -----------
monday A1
tuesday A2
wednesday C7
etc.
Is it possible? How can I access these values?
For VBA listbox you want to:
ColumnCount = 2
).ColumnWidths = ";0"
.BoundColumn = 2
) and the first column as textual (TextColumn = 1
).Have a procedure to add values:
Private Sub AddWithID(Text As String, ID As String)
ListBox1.AddItem Text
ListBox1.List(ListBox1.ListCount - 1, 1) = ID
End Sub
Now, for single-select listbox, you can use .Value
or .Text
to find out selected value/text.
For multi-select listbox, you can use .List(i, 0)
for text and .List(i, 1)
for value, where i
in an index of a row.
Another way... Using Collections.
Private HiddenValue As New Collection
Private Sub CommandButton1_Click()
AddItems "monday", "A1"
AddItems "tuesday", "A2"
AddItems "wednesday", "C7"
End Sub
Private Sub CommandButton2_Click()
MsgBox "Shown Value :" & ListBox1.List(ListBox1.ListIndex) & vbNewLine & _
"Hidden Value " & HiddenValue(ListBox1.ListIndex + 1)
End Sub
Private Sub AddItems(Text As String, ID As String)
ListBox1.AddItem Text
HiddenValue.Add ID
End Sub
SNAPSHOTS
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