Excel 2010: Excel VBA: UserForm ListBox, return selected value to a variable

ajay_gajree

Well-known Member
Joined
Jul 16, 2011
Messages
518
Code:
Private Sub UserForm_Initialize()
Dim rCell As Range
Dim rRng As Range

    Set rRng = shtList.Range("tbl1[Heading 1]")
        For Each rCell In rRng.Cells
            lboList.AddItem rCell.Value
        Next rCell
End Sub

Private Sub cmdList_Click()
Dim lItem        As Long
Dim YourValue As String

    For lItem = 0 To lboEmails.ListCount - 1
        
        YourValue = lboEmails.Value


        'USE YOUR VALUE IN SOME WAY
            
    Next
    Unload Me
End Sub

Hi All

I have a Userform with a List Box that allows Multiple Selections, this work fine loading values into it from a range in my workbook.

My issue is, how to I assign a selected value to to a variable in the code so I can use it in some way and then loop to use the next selected value?

Can't work this out! Help!

Hope that is clear!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You can't return a multi-select list to one variable, you have to loop through the list and check item by item to see if it's been selected.

Code:
Dim i as Long

For i = 1 to Listbox1.ListCount
   If Listbox1.Selected(i) Then
       'your code here
   End If
Next i

Of course, you could store the selected values in to a dynamic array or something like that if you need them frequently.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,624
Members
449,240
Latest member
lynnfromHGT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top