View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default select items in a list box with a macro?

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




Fan924 wrote:

Instead of a mouse, can you select items in a list box with a macro?


--

Dave Peterson