Solved

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

Posted on 2013-01-30
6
517 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

825 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