PDA

View Full Version : VBA: How to use listbox properly.



HeloNatebour
05-10-2019, 04:03 PM
Hi! Thanks for all the help received, i've learned a lot from you guys.

Today i want to know how to use the listbox, i need to do an operation with the listbox selected option example:


Density list:

[Listbox: Gasoline
Water
Air]

I know the values of Gasoline = 670, Water = 1000 and air = 1,3.

Operation = 10 * (Selected Listbox Item.Value)


How do i assign the "values" of each item to the listbox and how can i manage to multiply "10 * Variable" where variable is the value of the selected Listbox item!
Or is easiest to use a Combobox instead?

Thanks in advance

Bob Phillips
05-11-2019, 03:47 AM
You set the columncount of the listbox to 2 and then put the text in one list, the values in another.


Private Sub UserForm_Activate()

With Me.ListBox1

.ColumnCount = 2
.MultiSelect = fmMultiSelectSingle

.AddItem "Gasoline"
.List(.ListCount - 1, 1) = 670
.AddItem "Air"
.List(.ListCount - 1, 1) = 1000
.AddItem "Water"
.List(.ListCount - 1, 1) = 1.3
End With
End Sub

and get the items with something like


With Me.ListBox1

MsgBox .Value & " costs " & .List(.ListIndex, 1) * 10
End With