Solved

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

Posted on 2013-01-30
6
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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