How to stop user from selecting more than a specific amount of items in a listbox in VBA?

Mazon

New Member
Joined
Mar 4, 2016
Messages
2
Hi


I have a ListBox control ListBox1 and my goal is that the user should only be able to 3 items in ListBox1. I don't want the user to be able to select a 4th item.

My question is how do I stop the user from selecting the 4th item in the ListBox control?

This is what I've tried so far:

I'm using the change event of the ListBox control to do the following:

1) I have made a loop that checks how many items are selected. The amount of items selected is stored in the variable intAmtSelected.
2) If 3 or more items are selected the ListBox control is disabled.
3) If less than 3 items are selected the ListBox control is again enabled.
4) If more than 3 items are selected it displays an error message

My problem is that the ListBox.Enabled function does not work, the user can still select more than 3 items in ListBox1. Anyone know of alternative ways to solve this?


Code:
Private Sub ListBox1_Change()
Dim intAmtSelected As Integer
Dim intA As Long

'Checks if there is more than 3 items selected
For intA = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(intA) = True Then
       intAmtSelected = intAmtSelected + 1
    End If
Next intA


'If more than 3 items are selected disables the ListBox control and prompts an error message
If intAmtSelected > 3 Then
    MsgBox "You are not allowed to select more than 3 items in total."
End If

'If more than 3 items are selected disable the listbox
If intAmtSelected >= 3 Then
    ListBox1.Enabled = False
End If

'if less than 3 items are selected enable the lisbox
If intAmtSelected < 3 Then
    ListBox1.Enabled = True
End If

End Sub
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

skywriter

Well-known Member
Joined
Feb 15, 2014
Messages
1,642
Don't you have a command button that runs the final code that does something with the selected items in the list box, like write them to a worksheet?

I would have that code check the number of items and give them a message that they selected too many items and send them back to the list box or just exit the code after telling them to make the change and try again.
 

Mazon

New Member
Joined
Mar 4, 2016
Messages
2
Don't you have a command button that runs the final code that does something with the selected items in the list box, like write them to a worksheet?

I would have that code check the number of items and give them a message that they selected too many items and send them back to the list box or just exit the code after telling them to make the change and try again.

Yes I do, but I wanted to solve this specifically with the change event of the ListBox control. I managed to solve this by creating a another loop that deselects the latest item if the amount of items selected is greater than 3.l
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,325
Members
414,053
Latest member
Dual Showman

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
Top