I created a small userform with a listbox, label and two commandbuttons (cancel
& ok) on it.
This was the code behind the userform:
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub ListBox1_Change()
Dim MaxSelections As Long
Dim iCtr As Long
Dim sCtr As Long
MaxSelections = 8
sCtr = 0
Me.Label1.Caption = ""
For iCtr = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(iCtr) Then
sCtr = sCtr + 1
If sCtr > MaxSelections Then
Beep
Me.Label1.Caption = "Too many Selected"
Exit For
End If
End If
Next iCtr
Me.CommandButton2.Enabled = ((sCtr <= MaxSelections) And (sCtr > 0))
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
For iCtr = 1 To 15
.AddItem "asdf" & iCtr
Next iCtr
End With
Me.Label1.Caption = ""
'make them select at least one item
Me.CommandButton2.Enabled = False
End Sub