Solved

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

Posted on 2013-01-30
6
495 Views
Last Modified: 2013-02-03
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
0
Comment
Question by:ctownsen80
6 Comments
 
LVL 13

Accepted Solution

by:
Shanan212 earned 500 total points
ID: 38836373
Private DisableEvents As Boolean

Private Sub ListBox1_Change()
    Dim i As Long
    
    If DisableEvents = True Then Exit Sub

    If Me.ListBox1.Selected(0) Then
        
        DisableEvents = True

        For i = 1 To ListBox1.ListCount - 1
                   
        Application.DisplayAlerts = False
            ListBox1.Selected(i) = True
        Next i
    End If
    DisableEvents = False


End Sub

Open in new window


Right click on the sheet tab (Step 1) and select view code

In the code pane, paste the above code and let me know
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38836409
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
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38836456
Try this file
Copy-of-ee-example.xlsm
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:shorvath
ID: 38839143
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
0
 
LVL 9

Expert Comment

by:shorvath
ID: 38839355
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

0
 

Author Closing Comment

by:ctownsen80
ID: 38850034
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now