Identify selected item in Listbox

  • Hi All
    Im trying to pick an item from a listbox and have a command button that, when clicked will open a Worksheet!
    I understand how to pick an item when the listbox has been populated using the 'AddItem' method, however the listbox is dynamically populated, so the selection will change!
    So I would like some help on the correct syntax, (..and if possible an explanation to the code used, as I dont just want to 'copy & Paste'.. it would good to understand [Blocked Image: http://www.mrexcel.com/forum/images/smilies/icon_smile.gif] )



    Here's the code for populating the listbox

    Code
    [/SIZE] 	Private Sub UserForm_Initialize()Dim rngName As Range    Dim ws As Worksheet    Dim i As Integer    Set ws = Worksheets("Events")    For i = 1 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row Step 1        If ws.Cells(i, 1).Value <> vbNullString Then Me.lbCurrentEvents.AddItem ws.Cells(i, 1).Value    Next i                lbCurrentEvents.RowSource = "Events!A2:E" & Range("E" & Rows.Count).End(xlUp).RowlbCurrentEvents.ListIndex = lbCurrentEvents.ListIndexEnd Sub[size=14]

    [/SIZE]



    Thank you very much!


    D

  • Re: Identify selected item in Listbox


    Quote

    I understand how to pick an item when the listbox has been populated using the 'AddItem' method, however the listbox is dynamically populated, so the selection will change!


    I've never heard that how a listbox is populated will change how you retrieve an item from it.


    Retrieving an item from a list box or items if from a multi-select list box is a very common question that could probably be found by searching this board or googling the question.

    Bruce :cool:

  • Re: Identify selected item in Listbox


    Quote from skywriter;773045

    I've never heard that how a listbox is populated will change how you retrieve an item from it.


    Retrieving an item from a list box or items if from a multi-select list box is a very common question that could probably be found by searching this board or googling the question.


    Hi Skywriter..


    Thanks for reply... Perhaps the question was structured incorrectly..


    The Listbox is populated dynamically, using 'LISTBOX1.ROWSOURCE.' , if it was a case of a simple 'Static' list, thats fine, but its not.. the list could be 'Party' 'Boat Trip' & 'Shopping' today, tomorrow it could be 'Theatre' 'Skating' & 'Beach Trip'. But it will be placed within the same range within the WS
    what I needed to know is how do I structure the syntax so that it knows when Ive selected 'Boat trip' or 'Party' in the listbox as its changing!!


    ....and believe me...I have googled a solution for ages, If I would have found what I was looking for, I doubt I would be here asking!!, :-(, Everything have seen or read so far is not quite what I need.



    Code
    lbCurrentEvents.RowSource = "Events!A2:E" & Range("E" & Rows.Count).End(xlUp).Row 
    lbCurrentEvents.ListIndex = lbCurrentEvents.ListIndex.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!