Link to home
Start Free TrialLog in
Avatar of ctownsen80
ctownsen80Flag for Afghanistan

asked on

Check ALL Values when Select "(All)" in Excel ListBox

I have a list box in excel. This list box contains values of Names, and at the top of the list is the value "(All)". RIght now "All" is part of the selections. Currently, if a user wants to select ALL names displayed in the listbox, they must manually go through and click each and every name to accomplish this. I would like them to be able to click "(All)", and after which, all values in the list box would be checked for selection.

Refer to each step in the attached sheet to show what I am trying to accomplish.
Let me know if you have any follow up questions.


Any takers?

Thanks
ee-example.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Shanan212
Shanan212
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Saqib Husain
I think there is going to be a problem with the application of (All)

Since there is no way to determine which item was last selected and there is no setting for an item other than true or false this system is going to produce undesirable reactions.

For example if the (all) button is selected then you would not be able to unselect any other button. You will have to unselect (All) before you can unselect any other item.

Also you will not be able to unselect All by unselecting the (All) item.

You should consider using a userform instead of a listbox. Alternatively you can also consider inserting a separate button which can be used to select/unselect ALL
No buttons required...

Just paste this code into Sheet1


Private DisableEvents As Boolean

Private Sub ListBox1_Change()


If DisableEvents = True Then Exit Sub

If ListBox1.ListIndex = 0 Then

    DisableEvents = True
    If ListBox1.Selected(0) = True Then
        For i = 1 To ListBox1.ListCount - 1
            ListBox1.Selected(i) = True
        Next i
    Else
        For i = 1 To ListBox1.ListCount - 1
            ListBox1.Selected(i) = False
        Next i
    End If
End If

DisableEvents = False

End Sub

Open in new window

ee-example.xlsm
Even better.   I have added a bit of code for the "all but one" scenario.  If the user wants to include all the names but one, they can now select ALL and then uncheck the one name they don't want.  Also the code tests to see if all the names are selected and sets the ALL checkbox accordingly.

Private DisableEvents As Boolean

Private Sub ListBox1_Change()
Dim AllChecked As Boolean

If DisableEvents = True Then Exit Sub

If ListBox1.ListIndex = 0 Then

    DisableEvents = True
    If ListBox1.Selected(0) = True Then
        For i = 1 To ListBox1.ListCount - 1
            ListBox1.Selected(i) = True
        Next i
    Else
        For i = 1 To ListBox1.ListCount - 1
            ListBox1.Selected(i) = False
        Next i
    End If
Else
    If ListBox1.Selected(0) = True Then
        ListBox1.Selected(0) = False
    Else
        AllChecked = True
        For i = 1 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) = False Then
                AllChecked = False
                Exit For
            End If
        Next i
        
        If AllChecked = True Then
            ListBox1.Selected(0) = True
        End If
    End If
End If

DisableEvents = False

End Sub

Open in new window

Avatar of ctownsen80

ASKER

All suggestions and codes were great and I much appreciate the effort in working on them. However, I think solution will best support my goal.

Thank you again to everyone,

Chris