VBA Loop thru Listbox entries ?

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?
 
L

losmac

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
 
D

David

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

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top