EASY: vba: Looking through a listbox and selecting

2019-08-24 03:19发布

问题:

I know this is something really simple and it's slipping my mine right now. how do I loop though a listbox and select the contents?

Here's the code I've made right now:

Dim allGrps As Integer
Dim i As Integer
allGrps = lstAllGroups.ListCount
For i = 1 To allGrps
lstAllGroups
Next

回答1:

Keep in mind that the listbox is zero-indexed so you've got to subtract one off from i:

For i = 1 To allGrps
    lstAllGroups.Selected(i - 1) = True
Next i

Also, make sure you've got SelectSheetsList.MultiSelect = fmMultiSelectMulti in order to be able to select multiple items in the list.

See the ListBox documentation for more info.