Multiselect Listbox

OldManDemosthenes

New Member
Joined
Apr 19, 2011
Messages
38
I am creating a userform with 2 multiselect listbox. Can anyone tell me how to make the selection(s) appear in column A for the first listbox and column B for the 2nd? Selection 1 in row 1, selection 2 in row 2, etc...

If possible I would also would like the userform to make sure that the user selects at least one option from either of the listbox. I've done this for the first listbox but I don't know how to incorporate both listbox at once.

My code so far is posted below

Thanks for the help!


Private Sub cbRun_Click()

Dim i As Long, msg As String, Check As String
With lbCity
For i = 0 To .ListCount - 1
If .Selected(i) Then
msg = msg & .List(i) & vbNewLine
End If
Next i
End With
If msg = vbNullString Then

MsgBox "You must select a Trade Area"
Exit Sub
Else
Check = MsgBox("You selected:" & vbNewLine & msg & vbNewLine & _
"Are you happy with your selections?", _
vbYesNo + vbInformation, "Please confirm")
End If

If Check = vbYes Then

If obCapita.Value = True Then Cells(2, 4) = "Capita" Else
If obHH.Value = True Then Cells(2, 4) = "Household"

If obCensus.Value = True Then Cells(3, 4) = "Census" Else
If obDOF.Value = True Then Cells(3, 4) = "DOF"

frmInput.Hide

Else
For i = 0 To lbCity.ListCount - 1
lbCity.Selected(i) = False
Next
End If

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
hi and welcome to the board

from your code:

Code:
....
For i = 0 To .ListCount - 1
    If .Selected(i) Then
        cells(i,1)= .List(i) 
    End If
Next i
....

will put selections to col A
 
Upvote 0
Thanks for the quick response Diddi. I appreciate it very much

I entered in your code with mixed results. Most of the time I get an error with "cells(i,1)= .List (i)" highlighted. When the code works, it includes blank cells between selections. If I select the 3, 4, and 8 options from the list, A1:A2 shows 3,4, A3:A5 are blank, and A6 is 8.

Do you know how to solve either of these issues? I would include my worksheet but I cannot add attachments.

I inserted your code directly below "If Check = vbYes Then" from my previous code:

....
If Check = vbYes Then

With lbCity​
For i = 0 To .ListCount - 1​
If .Selected(i) Then​
cells(i,1)= .List(i)​
End If​
Next i​
End With​
.....

Thanks again for the help!
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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