Listbox - limited selection problem

  • Thread starter Thread starter Przemek
  • Start date Start date
P

Przemek

Hi

I've following, I believe easy to solve but not for me, problem. I have put
in my UserForm listbox. I've set Mulitselect property of Listbox to
fmMultiSelectMulti but than I can select non limited number of items and I
want to able to select only two of them. Do you know perhaps how to limit
this?

Thanx in advanced.
P.J.
 
Do you have a "ok/continue/proceed" button on your form?

If yes, then I think I'd just keep that button disabled until they had exactly
two items selected. Let them choose as many as they want (with a warning), but
don't let them proceed:

Option Explicit
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub ListBox1_Change()
Dim iCtr As Long
Dim TotSelected As Long

TotSelected = 0
With Me.ListBox1
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
TotSelected = TotSelected + 1
End If
Next iCtr
End With

Me.btnOK.Enabled = False
Select Case TotSelected
Case Is = 2: Me.btnOK.Enabled = True
Case Is > 2: MsgBox "Please select exactly two items"
End Select

End Sub
Private Sub UserForm_Initialize()
Me.btnOK.Enabled = False
End Sub
 
Back
Top