Get selected item in ListBox vba

Get selected item in ListBox vba
Reafidy
Administrator
Likes Received4Points13,393Trophies2Posts4,377
  • Dec 5th 2007
  • #4

Re: Retrieve Selected Items In Listbox


This will loop through the items in the listbox and return them to the worksheet.


Code
  1. Private Sub CommandButton1_Click()
  2. Dim lItem As Long
  3. For lItem = 0 To Me.ListBox1.ListCount - 1
  4. If Me.ListBox1.Selected(lItem) Then
  5. Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Me.ListBox1.List(lItem)
  6. End If
  7. Next lItem
  8. End Sub
Get selected item in ListBox vba
Andy Pope
OzMVP (Roobarb)
Likes Received1Points33,975Trophies2Posts11,304
  • Dec 6th 2007
  • #9

Re: Retrieve Selected Items In Listbox


As the control is on a worksheet you need to include a reference to it.


[vba]
Sub GetListboxItems()

Dim iListCount As Integer
Dim iRow As Integer
Dim rStartCell As Range

Set rStartCell = Sheets("Sheet1").Range("F65536").End(xlUp).Offset(1, 0)

With ActiveSheet.Shapes("ListBox1").OLEFormat.Object.Object

For iListCount = 0 To .ListCount - 1
If .Selected(iListCount) = True Then
.Selected(iListCount) = False
iRow = iRow + 1
rStartCell.Offset(iRow).Value = .List(iListCount, 0)

End If
Next iListCount
End With

Set rStartCell = Nothing


End Sub
[/vba]

[h4]Cheers
Andy
[/h4]