Results 1 to 2 of 2

Thread: VBA: How to use listbox properly.

  1. #1

    Question VBA: How to use listbox properly.

    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

    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
    Last edited by HeloNatebour; 05-10-2019 at 04:19 PM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Apr 2005
    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
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts