vba help - select all items from listbox1

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Need help , if user selects All_Fruits then
all fruits of listbox1. should get selected. how to achieve this


VBA Code:
Private Sub UserForm_Initialize()

ListBox1.AddItem "Apple"
ListBox1.AddItem "Mango"
ListBox1.AddItem "Guava"
ListBox1.AddItem "All_Fruits"

End Sub




1630399372116.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Activate multi select list property and use mousedown and mouseup event.
VBA Code:
Dim vN As Integer

Private Sub UserForm_Initialize()

    ListBox1.AddItem "Apple"
    ListBox1.AddItem "Mango"
    ListBox1.AddItem "Guava"
    ListBox1.AddItem "All_Fruits"
    ListBox1.MultiSelect = 1

End Sub

Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    
    For vN = 0 To ListBox1.ListCount - 1
         ListBox1.Selected(vN) = False
    Next vN
        
End Sub

Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    If ListBox1.Selected(ListBox1.ListCount - 1) = True Then
        For vN = 0 To ListBox1.ListCount - 1
            ListBox1.Selected(vN) = True
        Next vN
    Else
        For vN = 0 To ListBox1.ListCount - 1
            ListBox1.Selected(vN) = False
        Next vN
        ListBox1.Selected(ListBox1.ListIndex) = True
    End If
    
End Sub
 
Upvote 0
Hi ExcelMax,

the code is working, only thing is that I am unable to select two values,
Say Apple and Mango, it is allowing to select single value.


one more help,
how to check ListBox1.list , if user has selected All_Fruits.



1630405637512.png
 
Upvote 0
How about this...
Select all fruits or select fruits one by one.
Reset selection by double click on "All fruits".
Use only mouse up event.
VBA Code:
Dim vN As Integer, vAll As Boolean

Private Sub UserForm_Initialize()
    
    With ListBox1
        .AddItem "Apple"
        .AddItem "Mango"
        .AddItem "Guava"
        .AddItem "All_Fruits"
        .MultiSelect = 1
    End With
    
End Sub

Private Sub ListBox1_MouseUp(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    With ListBox1
        If vAll = False Then
            If .ListIndex = .ListCount - 1 Then
                For vN = 0 To .ListCount - 1
                    .Selected(vN) = True
                Next vN
                vAll = True
             Else
                .Selected(.ListCount - 1) = False
                .Selected(.ListIndex) = True
             End If
        Else
            If .ListIndex = .ListCount - 1 Then
                For vN = 0 To .ListCount - 1
                    .Selected(vN) = False
                Next vN
                .Selected(.ListIndex) = True
                vAll = False
             End If
        End If
    End With
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top