I use an array passed to a public sub to select items in a multi select listbox.
Code:
Public Sub SelectLBX(lbx As ListBox, strIN As String)
'Clears the listbox and then selects the values in the array passed in
On Error GoTo SelectLBX_Error
Call ClearList(lbx)
Dim varItm As Variant
Dim i As Integer
Dim Y As Integer
varItm = Split(strIN, ",")
For Y = 0 To UBound(varItm)
For i = 0 To lbx.ListCount - 1
If lbx.ItemData(i) = varItm(Y) Then
lbx.Selected(i) = True
Exit For
End If
Next i
Next Y
On Error GoTo 0
Exit Sub
SelectLBX_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure SelectLBX of Module modLBX"
End Sub
The following sub is called in the sub above, so including it here.
Code:
Function ClearList(lst As ListBox) As Boolean
On Error GoTo Err_ClearList
'Purpose: Unselect all items in the listbox.
'Return: True if successful
'Author: Allen Browne. http://allenbrowne.com June, 2006.
Dim varItem As Variant
If lst.MultiSelect = 0 Then
lst = Null
Else
For Each varItem In lst.ItemsSelected
lst.Selected(varItem) = False
Next
End If
ClearList = True
Exit_ClearList:
Exit Function
Err_ClearList:
Resume Exit_ClearList
End Function