VBA Loop thru Listbox entries ?

  • Thread starter Thread starter David
  • Start date Start date
D

David

Is it possible in VBA to loop thru the listbox entries.
How do I detect which entry I am currently on and how do I
detect the end of the entries?
 
Private Sub CmdEnumListBoxVal_Click()
Dim i As Long

'for looping listbox entries
For i = 0 To Me.ListBox1.ListCount - 1
'select if You want
Me.ListBox1.Selected(i) = True
MsgBox Me.ListBox1.Column(0, i), vbOKOnly, "Item
number: " & i + 1
Next i

'current selection
i = Me.ListBox1.ListIndex
MsgBox Me.ListBox1.Column(0, i), vbOKOnly, "Item
selected: " & i + 1

'end of listbox entries
i = Me.ListBox1.ListCount - 1
MsgBox "Last row: " & i + 1, vbOKOnly, "End of list"
End Sub
 
Thanks a million!
-----Original Message-----

Private Sub CmdEnumListBoxVal_Click()
Dim i As Long

'for looping listbox entries
For i = 0 To Me.ListBox1.ListCount - 1
'select if You want
Me.ListBox1.Selected(i) = True
MsgBox Me.ListBox1.Column(0, i), vbOKOnly, "Item
number: " & i + 1
Next i

'current selection
i = Me.ListBox1.ListIndex
MsgBox Me.ListBox1.Column(0, i), vbOKOnly, "Item
selected: " & i + 1

'end of listbox entries
i = Me.ListBox1.ListCount - 1
MsgBox "Last row: " & i + 1, vbOKOnly, "End of list"
End Sub

.
 
Back
Top