adjusting code populate data in listbox based on multiple comboboxes

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
hi

I need updating this code . currently filter data based on names in combobox1 linked with col G . now I would add two comboboxes (combobox2= month, combobox3= year) . the month in combobox2 should be 1,2... etc and combobox 3 = 2021 they are both linked with COL D is date (dd/mm/yyyy) . so three comboboxes should link for each other of them
VBA Code:
Private Sub ComboBox1_Change()
  Dim f As Range, r As Range, cell As String
 
  ListBox1.Clear
  If ComboBox1.Value = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
 
  Set r = sh.Range("g4:g" & sh.Range("G" & Rows.Count).End(xlUp).Row)
  Set f = r.Find(ComboBox1.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      With ListBox1
        .AddItem sh.Range("A" & f.Row).Value
        .List(.ListCount - 1, 1) = sh.Range("B" & f.Row).Value 'Data from Col B
        .List(.ListCount - 1, 2) = sh.Range("C" & f.Row).Value 'Data from Col C
        .List(.ListCount - 1, 3) = sh.Range("D" & f.Row).Value 'Data from Col D
        .List(.ListCount - 1, 4) = sh.Range("E" & f.Row).Value 'Data from Col E
        .List(.ListCount - 1, 5) = sh.Range("F" & f.Row).Value 'Data from Col f
        .List(.ListCount - 1, 6) = sh.Range("G" & f.Row).Value 'Data from Col g
      End With
      Set f = r.FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
End Sub


Private Sub UserForm_Initialize()
  Dim i As Long, dic As Object
 
  Set sh = Sheet3
  Set dic = CreateObject("Scripting.Dictionary")
 
  For i = 4 To sh.Range("G" & Rows.Count).End(xlUp).Row
    dic(sh.Range("G" & i).Value) = Empty
  Next
  ComboBox1.List = dic.keys
  ListBox1.ColumnCount = 7
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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