Listbox with multiple results

OldManDemosthenes

New Member
Joined
Apr 19, 2011
Messages
38
I am having trouble displaying the results of a multiselect listbox. I would like the results to appear in column D rows 1, 2, etc... for however many selections I make. If possible, it would also be nice to leave row 1 blank as a header row.

I have a code that returns my selection with blank rows. For example, if I select the 5th, 10th, and 12th options in the listbox, the results are displayed in D5, D10, and D12.

Also, if I try to rerun the userform with different listbox selections, I get an error. I think this is because my selection list are not clearing, but honestly I have no clue.

My code is below. I have an output that lists all of my selections in a string in one cell. This part of the code runs perfectly and there are no errors associated with it.

Code:
Private Sub cbRun_Click()
Dim i As Long, cty As String

[INDENT]With lbCity[/INDENT]
[INDENT][INDENT]For i = 0 To .ListCount - 1[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]If .Selected(i) Then[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT][INDENT]cty = cty & ", " & .List(i, 1)[/INDENT][/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]End If[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT]Next i[/INDENT][/INDENT]
[INDENT]End With[/INDENT]
[INDENT]Cells(5, 1) = Left(cty, Len(cty) - 2)[/INDENT]
[INDENT]With lbCity[/INDENT]
[INDENT][INDENT]For i = 0 to .ListCount - 1[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]If .Selected(i) then[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT][INDENT]Cells(i,5)=.List(i)[/INDENT][/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]End If[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT]Next i[/INDENT][/INDENT]
[INDENT]End With[/INDENT]
Unload Me
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try...

Code:
[font=Verdana][color=darkblue]Private[/color] [color=darkblue]Sub[/color] cbRun_Click()
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]With[/color] lbCity
        [color=darkblue]For[/color] i = 0 [color=darkblue]To[/color] .ListCount - 1
            [color=darkblue]If[/color] .Selected(i) [color=darkblue]Then[/color]
                Cells(Rows.Count, "D").End(xlUp)(2) = .List(i, 1)
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    Unload Me
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

If, however, the listbox has only a single column, replace...

Code:
Cells(Rows.Count, "D").End(xlUp)(2) = .List(i, 1)

with

Code:
Cells(Rows.Count, "D").End(xlUp)(2) = .List(i)
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,706
Members
452,939
Latest member
WCrawford

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