How to select specific data from listbox?

Excel VBA question: How can I use vba to access a listbox?

  • I can't find the answer to this anywhere. I have a listbox displayed in my workbook (note: in my workbook, not in a form). This listbox has 10 values in it, and I have given the option of selecting more than one value. I am now writing a macro that accesses the data chosen by the user. I cannot find a way to access the list box. If I set the list box to only allow one selection then there is a Cell Link option that will output the results to another cell, however when I set the list box to multi select it will only output a 0 to this cell, thus I can't access the information this way. I need a way to get at this information via VBA. I'm good at VBA and excel, so a quick and simple example that works is all I need. Note: the name of my list box is 'MetricLB' Thanks! David

  • Answer:

    The Cell Link is ignored for a MultiSelect List Box. You have to access the selected items in code. Test the Selected property of each item in the List to determine which items are selected. Private Sub ListChoices() Dim i As Integer Dim cnt As Integer ' Get the number of items in the list cnt = ListBox1.ListCount ' Loop through the items, checking each one ' to see if it is selected For i = 0 To (cnt - 1) If ListBox1.Selected(i) = True Then Debug.Print ListBox1.List(i) End If Next i End Sub Additional Resources: Look at Excel VBA Help for the ActiveX ListBox Object - Hammer

davidparks21-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.