-
Jan 10th, 2014, 02:21 PM
#1
Thread Starter
New Member
[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!
-
Jan 10th, 2014, 03:01 PM
#2
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.
-
Jan 11th, 2014, 12:29 AM
#3
Re: [Excel] Using Selected Values in Listbox from Userform
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
I always get a little confused when using the Do While and Loop elements.
i do not see any do while
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|