How to only allow 1 listbox selection at a time?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have 2 listboxes (ListBox1 and ListBox2).

When I select something in listbox1, listbox2 deselects and vice versa.

I have tried these in listbox1 and listbox2 change and click events:
Code:
ListBox1.Selected(ListBox1.ListIndex) = False
and
Code:
ListBox2.Selected(ListBox2.ListIndex) = False

They work for the first selection but if I select in the same listbox twice it triggers the following error:

Run time error 380 - Could not set the selected property. Invalid property value.

Does anyone know a way around this error?

Also I should mention I have a code in userform activate that highlights a value and that value can be in either listboxes but never both.

Any help would be appreciated.

Thanks

Dan
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could use code like this
Code:
Private Sub ListBox1_Change()
    If ListBox1.ListIndex <> -1 Then
        ListBox2.ListIndex = -1
    End If
    
    ' your existing code
End Sub

Private Sub ListBox2_Change()
    If ListBox2.ListIndex <> -1 Then
        ListBox1.ListIndex = -1
    End If

    ' your current code
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,953
Members
448,535
Latest member
alrossman

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