Originally Posted by
xld
... what happens if the specific item to turn off multi-select is not the first selected?
I would check which items in the listbox were selected whenever the listbox change event fired, and if the particular item were one of them, unselect any others, then change the .multiselect property.
Originally Posted by
xld
That doesn't seem to work, nor does disabling the Listbox.
There seems to be a problem with the display of the userform when you change the multiselect property to single, then use shift or ctrl to select an unselected item. The form knows that only the item you just selected is selected, but the list items are highlighted as if the multiselect property were still multi.
But we don't need to change the multiselect property, just not allow any other item to be selected. This code assumes a multiselect listbox on a userform, populated with items a, b, c, d. It works as a multiselect listbox until c is selected, then no matter what the user does, c stays selected and nothing else can be selected instead.
Option Explicit
Dim mbNoChanges As Boolean
Private Sub ListBox1_Change()
Dim iList As Long
If Not Application.EnableEvents Then Exit Sub
Application.EnableEvents = False
With Me.ListBox1
If Not mbNoChanges Then
For iList = 0 To .ListCount - 1
If .List(iList) = "c" And .Selected(iList) Then
mbNoChanges = True
End If
Next
End If
If mbNoChanges Then
For iList = 0 To .ListCount - 1
.Selected(iList) = (.List(iList) = "c")
Next
End If
End With
Application.EnableEvents = True
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.List = Array("a", "b", "c", "d")
End With
End Sub