only allow one list box to have a selection

deb

Active Member
Joined
Feb 1, 2003
Messages
400
I have two listboxes, lboFY and lboCY.

If the use makes a single select or multiselect on lboFY, I want to clear all selections that may have been selected in lboCY
and the other way around
If the use makes a single select or multiselect on lboCY, I want to clear all selections that may have been selected in lboFY

Can't have both listboxes with selections. Needs to one or the other.
 

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.
You would need these two scripts.
Change ListBox names as needed
Code:
Private Sub ListBox1_Click()
'Modified  7/25/2019  10:49:25 PM  EDT
ListBox2.ListIndex = -1
MsgBox ListBox1.Value
End Sub
Private Sub ListBox2_Click()
'Modified  7/25/2019  10:49:25 PM  EDT
ListBox1.ListIndex = -1
MsgBox ListBox2.Value
End Sub
 
Upvote 0
For a list box you would use something like:
Code:
Private Sub [COLOR=#333333]lboCY[/COLOR]_Click()
If Me.[COLOR=#333333]lboFY[/COLOR].MultiSelect = 0 Then
    Me.[COLOR=#333333]lboFY[/COLOR]= Null
Else
    For Each x In Me.[COLOR=#333333]lboFY[/COLOR].ItemsSelected
        Me.[COLOR=#333333]lboFY[/COLOR].Selected(x) = False
    Next x
End If
End Sub

and the same code on the alternative list box with the control names switched round.
 
Upvote 0
I am getting error 7777
You've used the ListIndex property incorrectly

any suggestions?
 
Upvote 0
Tried but this did nothing.
no errors and did not prevent selection of the other listbox
 
Upvote 0
Are you sure you have the correct control names?

I have quickly tested it using the exact names and it works no problem for me.
 
Upvote 0
Hi,
try following

Code:
Private Sub lboCY_Enter()
    ClearSelections Me.lboFY
End Sub


Private Sub lboFY_Enter()
    ClearSelections Me.lboCY
End Sub


Private Sub ClearSelections(ByVal ListBox As Object)
    Dim SelectedItem As Integer
    For SelectedItem = 0 To ListBox.ListCount
        ListBox.Selected(SelectedItem) = False
    Next SelectedItem
End Sub

Dave
 
Upvote 0
Are you refering to my posting post. If you Listbox is name ListBox1
I mentioned in my post you need to modify the listbox names. I tested my script and it worked for me. But it does not work on Multiselect listbox. Sorry

I am getting error 7777
You've used the ListIndex property incorrectly

any suggestions?
 
Last edited:
Upvote 0
@MAIT
Not sure if you realised, but this is in the Access Forum
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,288
Members
448,563
Latest member
MushtaqAli

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