select items in a list box with a macro?

D

Dave Peterson

You can use a line like this:

Me.ListBox1.ListIndex = 0

The first item in the listbox is item 0.

If you allow multiple selections, then this worked ok for me:

Option Explicit
Private Sub CommandButton1_Click()

Dim iCtr As Long
With Me.ListBox1
For iCtr = 0 To .ListCount - 1
If iCtr = 0 _
Or iCtr = 3 _
Or iCtr = 5 Then
.Selected(iCtr) = True
Else
.Selected(iCtr) = False
End If
Next iCtr
End With

End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
For iCtr = 1 To 10
.AddItem "A" & iCtr
Next iCtr
End With

End Sub
 
F

Fan924

Me.ListBox1.ListIndex = 0

Nice. I used it to scroll through the list with a small wait in
between.

Can I pull focus on the listbox and use the keyboards up & down
arrows. Is that programmable?
 
D

Dave Peterson

I'm not sure what pull focus means (.setfocus???), but you can change the top
item that you see in the listbox with a line like:

Me.ListBox1.TopIndex = 5
(0 is still the first item.)
 

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