Multiple Selections from Listbox into an Array, populated by a VBA form

championem31

New Member
Joined
Aug 16, 2013
Messages
3
So I have created a form for people to fill out, and I want them to be able to pick several options from a listbox. Then, when they save the form, I want the options they selected to populate in the next available row as an array. Here's what I have:

Private Sub btnsave_click()


Dim irow As Long
Dim ws As Worksheet
Set ws = Worksheets("13-14")




'find first row in database


With ws
irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row


.Range("A" & irow) = TypePresEvent.Value
.Range("B" & irow) = Topic.Value
.Range("D" & irow) = Class.Value
.Range("E" & irow) = Location.Value
.Range("F" & irow) = Attendance.Value
.Range("G" & irow) = Items.Value
.Range("H" & irow) = Notes.Value








End With
TypePresEvent.Value = ""
Topic.Value = ""
Class.Value = ""
Location.Value = ""
Attendance.Value = ""
Notes.Value = ""






Dim arrValues()
If Presenters.ListIndex <> -1 Then
For I = 0 To Presenters.ListCount - 1
If Presenters.Selected(I) Then
ReDim Preserve arrValues(X)
arrValues(X) = Presenters.List(I)
X = X + 1

End If

End If
Next I



Range("C65536").End(xlUp).Offset(1, 0).Select = Join(arrValues, ",")
With ws
End With
Presenters.Value = ""


End Sub



Needless to say, I keep getting errors (BLOCK IF END IF crap). Please help! Also, if you know a way to clear the selections off of the listbox after its submitted, that would be helpful too.

I'm self-taught at this, so please be nice.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Move Next I above the 2nd End If.

Actually you can remove the If that checks ListIndex, it's kind of irrelevant with a multi select listbox and doesn't tell you if anything's been selected.
Code:
Private Sub btnsave_click()
Dim irow As Long
Dim ws As Worksheet
Dim arrValues()

    Set ws = Worksheets("13-14")
    
    With ws
        ' find first blank row in database
        irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

        .Range("A" & irow) = TypePresEvent.Value
        .Range("B" & irow) = Topic.Value
        .Range("D" & irow) = Class.Value
        .Range("E" & irow) = Location.Value
        .Range("F" & irow) = Attendance.Value
        .Range("G" & irow) = Items.Value
        .Range("H" & irow) = Notes.Val

        For I = 0 To Presenters.ListCount - 1
            If Presenters.Selected(I) Then
                ReDim Preserve arrValues(X)
                arrValues(X) = Presenters.List(I)
                X = X + 1
                Presenters.Selected(I) = False
            End If

        Next I

        .Range("C" & irow).Value = Join(arrValues, ",")

    End With

    TypePresEvent.Value = ""
    Topic.Value = ""
    Class.Value = ""
    Location.Value = ""
    Attendance.Value = ""
    Notes.Value = ""

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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