Ken
Ken

Reputation: 19

How to assign items selected in listbox into each cell with For statement

I am going to use listbox to allow users to multiselect. However, I have no idea to put them items selected into each cell in a column. Here are my codes. I get stuck in the For statement as I know that it run go through the inner loop and then the outer loop. Anyone can help...Thanks

Private Sub lbaspectc_AfterUpdate()

For Each selected In Me.listbox.ItemsSelected

    For Each c3 In Worksheets("maintenance").Range(Cells(2, 7), Cells(, 7))  'Here i would like to start filling in from cell G2. I am not sure if the syntax is correct'

        c3.Value = selected.Value
    Next
Next

End Sub

Upvotes: 0

Views: 1632

Answers (1)

SJR
SJR

Reputation: 23081

Do you mean like this? You'll need to re-assign to the correct control.

Private Sub CommandButton1_Click()

Dim i As Long, j As Long

j = 2

With Me.ListBox1
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            Worksheets("maintenance").Cells(j, 7).Value = .List(i)
            j = j + 1
        End If
    Next i
End With

End Sub

Upvotes: 1

Related Questions