?
Solved

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

Posted on 2013-01-30
6
Medium Priority
?
548 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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 in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

650 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