Kelvs
Kelvs

Reputation: 107

Add multi selected items in listbox with multi column to an Excel worksheet

I am trying to add all selected items from a multi column listbox on a userform, to my Excel worksheet. These are the codes I'm currently using:

Dim lrow As Range
Dim wst As Worksheet
Dim lo As ListObject
Dim lr As ListRow

Application.ScreenUpdating = False

Dim xRow As Integer, intItem As Integer

Set wst = Sheets("TRAININGS PROFILE")
wst.Activate
wst.Range("B4").Select

For intItem = 0 To listbox1.ListCount - 1
If listbox1.Selected(intItem) = True Then

Set lo = wst.ListObjects(1)
Set lr = lo.ListRows.Add

ActiveCell.Offset(0, 3).Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
ActiveCell.Offset(0, 4).Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
ActiveCell.Offset(1, 0).Select
End If
Next intItem

Application.ScreenUpdating = True

This code works but the problem is it is only saving the last selected item from my listbox, it does not loop through each selected item. Please help.

Upvotes: 0

Views: 253

Answers (1)

Lee Li Fong
Lee Li Fong

Reputation: 259

Try change to code below: ActiveCell.Offset(0, 3).Value = Me.ListBox1.List(intItem , 0) ActiveCell.Offset(0, 4).Value = Me.ListBox1.List(intItem, 1)

Upvotes: 1

Related Questions