ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel VBA-Looping through Multiselection (https://www.excelbanter.com/excel-programming/290046-excel-vba-looping-through-multiselection.html)

jpendegraft

Excel VBA-Looping through Multiselection
 
Essentially, I am trying to return the value in a text box onto a
worksheet for all items that are selected.

The column is based off of a combo box drop down.
And the row is based off of the list box.

I am struggling to loop though a multiselection in a list box from a
userform. I can only get the last item on the selection to
populate......

My code is as follows:

For i = 1 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then _
r = ListBox1.ListIndex + 2
Select Case True
Case ComboBox1.ListIndex = 0: Cells(r, 6) = TextBox1.Value
Case ComboBox1.ListIndex = 1: Cells(r, 7) = TextBox1.Value
Case ComboBox1.ListIndex = 2: Cells(r, 8) = TextBox1.Value
Case ComboBox1.ListIndex = 3: Cells(r, 9) = TextBox1.Value
Case ComboBox1.ListIndex = 4: Cells(r, 10) =
TextBox1.Value
End Select
Next i


Any help would be greatly appreciated!


---
Message posted from http://www.ExcelForum.com/


Tom Ogilvy

Excel VBA-Looping through Multiselection
 
First, the first entry is zero not 1

Second. ListIndex won't change in your loop - you need to use "i" to get the
row. I made it I plus 2, but you may need to adjust the 2.

For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then _
r = i + 2
Select Case True
Case ComboBox1.ListIndex = 0: Cells(r, 6) = TextBox1.Value
Case ComboBox1.ListIndex = 1: Cells(r, 7) = TextBox1.Value
Case ComboBox1.ListIndex = 2: Cells(r, 8) = TextBox1.Value
Case ComboBox1.ListIndex = 3: Cells(r, 9) = TextBox1.Value
Case ComboBox1.ListIndex = 4: Cells(r, 10) = TextBox1.Value
End Select
Next i

--
Regards,
Tom Ogilvy


"jpendegraft " wrote in message
...
Essentially, I am trying to return the value in a text box onto a
worksheet for all items that are selected.

The column is based off of a combo box drop down.
And the row is based off of the list box.

I am struggling to loop though a multiselection in a list box from a
userform. I can only get the last item on the selection to
populate......

My code is as follows:

For i = 1 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then _
r = ListBox1.ListIndex + 2
Select Case True
Case ComboBox1.ListIndex = 0: Cells(r, 6) = TextBox1.Value
Case ComboBox1.ListIndex = 1: Cells(r, 7) = TextBox1.Value
Case ComboBox1.ListIndex = 2: Cells(r, 8) = TextBox1.Value
Case ComboBox1.ListIndex = 3: Cells(r, 9) = TextBox1.Value
Case ComboBox1.ListIndex = 4: Cells(r, 10) =
TextBox1.Value
End Select
Next i


Any help would be greatly appreciated!


---
Message posted from http://www.ExcelForum.com/





All times are GMT +1. The time now is 07:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com