[Excel] Using Selected Values in Listbox from Userform
I'm very close to getting my script to work the way I want it to. I always get a little confused when using the Do While and Loop elements.
I have a UserForm that has a ListBox that populates values pulled from the RowSource property. Also, I've set the MultiSelect value to "fmMultiSelectExtended" so that any configuraiton of multiple values can be selected in the ListBox. I want my script to go through each value, run some code for each selected, and then stop once each value has been used.
Here's what I have so far:
Code:
Dim I As Long
Dim J As Long
Dim ListValue As String
Dim Listbox1Variables() As String
ReDim Listbox1Variables(0 To ListBox1.ColumnCount - 1)
For J = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(J) Then
For I = 0 To ListBox1.ColumnCount - 1
Listbox1Variables(I) = ListBox1.Column(I, J)
Next I
ListValue = Join(Listbox1Variables, ",")
End If
'The list box contains titles and names together, this removes the title and returns the name. I then use the 'RealName' variable in my code.
Dim RealName As String
RealName = Right(ListValue, Len(ListValue) - InStrRev(ListValue, "- ") - 1)
''''run some code here, using the RealName variable'''
Next J
Right now, it's not moving to the next ListBox value. It only does the first ListBox value, and does an infinite loop for the first value. I have to use CTRL+Break to stop the code. I'm not sure where to adjust the code. Thanks for any help!
Re: [Excel] Using Selected Values in Listbox from Userform
Add a watch for Listbox1(J) right after the "If Listbox1.Selected(J) Then" to verify whether or not you are actually moving through the selected values.
Re: [Excel] Using Selected Values in Listbox from Userform
Quote:
If ListBox1.Selected(J) Then
For I = 0 To ListBox1.ColumnCount - 1
Listbox1Variables(I) = ListBox1.Column(I, J)
Next I
ListValue = Join(Listbox1Variables, ",")
End If
looks like the rest of the code is run regardless of whether the list item is selected, so would reuse listvalue for last selected item
maybe move end if to above next J
even so i do not see any reason why the loop would iterate more times than the number of items in listbox, post the rest of the code
Quote:
I always get a little confused when using the Do While and Loop elements.
i do not see any do while