ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   select items in a list box with a macro? (https://www.excelbanter.com/excel-programming/438278-select-items-list-box-macro.html)

Fan924

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

Dave Peterson

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

Fan924

select items in a list box with a macro?
 
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?

Dave Peterson

select items in a list box with a macro?
 
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.)

Fan924 wrote:

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?


--

Dave Peterson


All times are GMT +1. The time now is 09:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com