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
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top