Link to home
Start Free TrialLog in
Avatar of Saha H
Saha H

asked on

Insert checkbox inside the combobox list for multiple selection

How to insert CheckBoxes to the list present in ComboBox?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

What is your environment, VB Classic (VB6), .Net or VBA?. You have indicated all three in your list of topics.

In VBA you can use a ListBox with ListStyle = fmListStyleOption.
VBA UserForm ComboBox does not have this Property ListBoxes do. You can set this when the UserForm Initialises or manually in it's Properties Window.
Private Sub UserForm_Initialize()
Me.ListBox1.ListStyle = fmListStyleOption

End Sub

Open in new window


 I'm sure the same applies to VB.Net


ListBox Class
Avatar of Saha H
Saha H

ASKER

OK thank you...
Avatar of Saha H

ASKER

How can i filter data based on multi selection values in Listbox using VBA?
We still do not know what coding language you are using. Also, this is a further question and should be asked on its own. This question needs closing by awarding the poinst after amending the Tags
Avatar of Saha H

ASKER

I am using VBA language only.
i got to know that checkbox options are only available with ListBoxes.
I am asking  when we select multiple options in that listbox , can we use those values to filter at a time.
You are  really still limited by two filter criteria for AutoFilter even using VBA this applies. You can build an array.

This is sample code to detect what has been selected in the ListBox, but as I said before, this should be a new question.

 Dim Msg As String
      Dim iX As Integer
      Msg = "You selected" & vbNewLine
      For iX = 0 To Me.ListBox1.ListCount - 1
          If ListBox1.Selected(iX) Then
              Msg = Msg & Me.ListBox1.List(iX) & vbNewLine
          End If
      Next i
      MsgBox Msg

Open in new window

This code assumes you have a ListBox1 and the data is on Sheet1 starting in A1

Sub FilterFromListBox()
    Dim iX As Integer
    Dim Var As Variant
    
    ReDim Var(0)
    Application.ScreenUpdating = False
    For iX = 0 To ListBox1.ListCount - 1
        If Me.ListBox1.Selected(iX) Then
            Var(UBound(Var)) = Me.ListBox1.List(iX)
            ReDim Preserve Var(UBound(Var) + 1)
        End If
    Next iX
    
    Sheet1.Range("A1").CurrentRegion.AutoFilter Field:=3, Criteria1:=Var, Operator:=xlFilterValues
    Application.ScreenUpdating = True
    
End Sub

Open in new window

Thanks Martin. It seems to be Excel VBA required
Avatar of Saha H

ASKER

i have one sheet in that, first we have to select year and next we have to select multiple  work week values from list box. for those selected values it has to filter the given table.
For year it is working fine but for multiple work week values it is not filtering properly.
Please find the attached dummy sheet.
Book.xlsm
Thank you for attaching a workbook, but it doesn't work at all. We need one that has the controls filled with data. Also when I went to open your form I got this message which is worrisome.User generated image
Do you intend using the UserForm or the sheet controls. A UserForm is more reliable than ActiveX controls on a sheet.

I've fixed some of your example. there is no button on the sheet, but let me know if this is along the correct lines.

Also, here's my example of filtering from a UserForm
FilterForm.xlsm
Book.xlsm
Avatar of Saha H

ASKER

Thank you for examples. I want with ActiveX controls only. But it is not filtering multiple selection of  workweeks.
I haven't included code for that in the example yet because it's not clear what you are doing.
Avatar of Saha H

ASKER

Please find the attached sheet....
Book.xlsm
Avatar of Saha H

ASKER

just i want to do  multiple selection and that should reflect in that table.
But it is not filtering for single selection also...
Your new example totally ignores what I did in the previous example.
Avatar of Saha H

ASKER

No no its same only just i have two more buttons for refresh and autofilter.
Avatar of Saha H

ASKER

sorry for typo mistake.
Only i have added two buttons . I need only multiple selection should be reflect in available table.
ASKER CERTIFIED SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Saha H

ASKER

OK thank you..
i have one doubt please can u explain me that. Why You kept this listbox procedure empty? Then how values are displaying in listbox?

Private Sub ListBox1_Click()

End Sub

Private Sub Worksheet_Activate()
''/// populate the ListBox and ComboBox
''///only use cells containing value, dynamic so data can be entered and reflect in list
    With Sheet3
        Me.ComboBox1.List = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp)).Value
        Me.ListBox1.List = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp)).Value
    End With
End Sub
Avatar of Saha H

ASKER

Sir, can you explain me this part of code...
You don't load the ListBox or ComboBox with the Change or Click event, if you do it means that every time you use it it will re-load.

My code loads it when the worksheet is activated, so it is refreshed with any changes to the List.
The code works as required