Load a combobox based on selection from another combobox - vba

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have two comboboxes and when I select say x from Combobox1, Then I would want to load into combobox2 all data in column B which have corresponding item matching the selection in combobox1.

The combobox1 will contain x, y, Z as test data and column c will house them.

My data start from row 4 the first three rows are headers

Thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
Private Sub ComboBox1_Click()
   Dim lst As Variant
   
   With Range("B4", Range("B" & Rows.Count).End(xlUp))
      lst = Filter(Evaluate("transpose(if(" & .Offset(, 1).Address & "=" & Chr(34) & Me.ComboBox1.Value & Chr(34) & "," & .Address & ",""#""))"), "#", False)
   End With
   Me.ComboBox2.List = lst
End Sub
 
Upvote 0
Very cool!!

But one issue is that instead of having the date show up in combobox2 as dd-mm-yy, it is showing its serial number.

How do I fix that?
 
Upvote 0
I observed that when I run the code and the sheet is not active, it fails to do the job

Though I have been able to qualify the sheet as:

Code:
Private Sub ComboBox1_Click()
   Dim lst As Variant, db  As Worksheet 

  Set db = Sheets("Sheet1")
   
   With db.Range("B4", db.Range("B" & Rows.Count).End(xlUp))
      lst = Filter(Evaluate("transpose(if(" & .Offset(, 1).Address & "=" & Chr(34) & Me.ComboBox1.Value & Chr(34) & "," & .Address & ",""#""))"), "#", False)
   End With
   Me.ComboBox2.List = lst
End Sub

What did I miss ?
 
Upvote 0
How about
VBA Code:
Private Sub ComboBox1_Click()
   Dim Lst As Variant
   Dim db As Worksheet
   Dim i As Long

   Set db = Sheets("Sheet1")
   With db.Range("B4", db.Range("B" & Rows.Count).End(xlUp))
      Lst = Filter(db.Evaluate("transpose(if(" & .Offset(, 1).Address & "=" & Chr(34) & Me.ComboBox1.Value & Chr(34) & "," & .Address & ",""#""))"), "#", False)
   End With
   For i = 0 To UBound(Lst)
      Lst(i) = Format(Lst(i), "dd-mm-yy")
   Next i
   Me.ComboBox2.List = Lst
End Sub
 
Upvote 0
Super cool! !!!

What's the difference between the click event and the change event?
 
Upvote 0
To some extent it depends on the combo settings.
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,417
Members
448,895
Latest member
omarahmed1

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