Limiting multi-select listboxes

David2e

New Member
Joined
Sep 26, 2007
Messages
47
I have a mult-column listbox where I'd like 4 items to be selected before it can be accepted.

Is it easy, or even possible to limit a multi-select listbox in this way?

It would also be great if a minimum number is possible (separate option).

Many thanks

David
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

swat2

Board Regular
Joined
Jul 23, 2009
Messages
173
Here's a function you can put in your code to make sure more than 4 items are selected.
Code:
Public Function ListBoxVerified() As Boolean

Dim SelectionCounter As Integer

SelectionCounter = 0
ListBoxVerified = False

For i = 0 To ListBox1.ListCount - 1

   If ListBox1.Selected(i) Then SelectionCounter = SelectionCounter + 1
 
Next i
    
If SelectionCounter >= 4 Then ListBoxVerified = True
    
End Function
Call this function with an if statement and throw an error if it comes back false
 

iliace

Well-known Member
Joined
Jan 15, 2008
Messages
3,543
Office Version
365, 2016, 2010
Platform
Windows
This function will count the number of items selected in a list box. The list box is passed as the parameter.

Code:
Private Function ListItemCount(lb As MSForms.ListBox) As Integer
  Dim i As Integer
  
  For i = 0 To lb.ListCount - 1
    If lb.Selected(i) Then ListItemCount = ListItemCount + 1
  Next i
End Function
 

David2e

New Member
Joined
Sep 26, 2007
Messages
47
Thanks guys!

Very helpful and I'll look at given that a shot soon.

Cheers
 

Watch MrExcel Video

Forum statistics

Threads
1,102,291
Messages
5,485,975
Members
407,523
Latest member
Talicius

This Week's Hot Topics

Top