Auto populate Drop down box with 2 different options

Ben171

Board Regular
Joined
Jul 2, 2021
Messages
88
Hi there, going to try and explain this clearly because it can get confusing,

I have a user form which uses various lookup methods to auto fill data. But for this explanation lets say i am just looking at 2 combo boxes on the form; Descriptioncmb and PartNocmb

We are looking at the following two columns in a data sheet:

1635234453452.png


The Description on the userform is chosen through the ComboBox options, which in this brief description, there will just be one option (pipe). then PartNocmb needs to be filled based on the description set.

I've done this through the use of a VLOOKUP;

VBA Code:
Dim MyTableArray As Range

Set MyTableArray = Sheets("Data").Range("A:B")

'vlookup
Me.PartNocmb = WorksheetFunction.VLookup(Me.Descriptioncmb, MyTableArray, 2, 0)

This works great, however we have now started to do conversions, meaning sometimes the part number me have a /5 on the end instead of a /3.

Is there a way to implement this to allow the option for a user to choose between the 1126/3 and 1126/5. I was thinking they could both be displayed in the PartNocmb and the user can select which one to use?

Hope that makes sense, really hard to explain!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hy ben171,
How about using autofilter to filter out your list and add matching results to combobox on the fly, instead of using vlookup?

VBA Code:
Private Sub ComboBox1_AfterUpdate()

'filter list based on Combobox1 value
Sheets("sheet1").UsedRange.AutoFilter Field:=1, Criteria1:=ComboBox1.Value

'focus filtered range
Set frng = Sheets("sheet1").Range("b2", Sheets("sheet1").Cells(Rows.Count, "b").End(xlUp)).SpecialCells(xlCellTypeVisible)

'clear Combobox2 list
ComboBox2.Clear

'Add visible items to Combobox2 List
For Each cell In frng
 ComboBox2.AddItem cell.Value
Next cell

'clear out applied filter
Sheets("sheet1").UsedRange.AutoFilter

'display Combobox2 dropdown
ComboBox2.DropDown

End Sub

hope this helps....

1635251846286.png
 
Upvote 0
Hy ben171,
How about using autofilter to filter out your list and add matching results to combobox on the fly, instead of using vlookup?

VBA Code:
Private Sub ComboBox1_AfterUpdate()

'filter list based on Combobox1 value
Sheets("sheet1").UsedRange.AutoFilter Field:=1, Criteria1:=ComboBox1.Value

'focus filtered range
Set frng = Sheets("sheet1").Range("b2", Sheets("sheet1").Cells(Rows.Count, "b").End(xlUp)).SpecialCells(xlCellTypeVisible)

'clear Combobox2 list
ComboBox2.Clear

'Add visible items to Combobox2 List
For Each cell In frng
 ComboBox2.AddItem cell.Value
Next cell

'clear out applied filter
Sheets("sheet1").UsedRange.AutoFilter

'display Combobox2 dropdown
ComboBox2.DropDown

End Sub

hope this helps....

View attachment 49870
This looks like a great idea, i will give this a go. Thanks!
 
Upvote 0
Hy ben171,
How about using autofilter to filter out your list and add matching results to combobox on the fly, instead of using vlookup?

VBA Code:
Private Sub ComboBox1_AfterUpdate()

'filter list based on Combobox1 value
Sheets("sheet1").UsedRange.AutoFilter Field:=1, Criteria1:=ComboBox1.Value

'focus filtered range
Set frng = Sheets("sheet1").Range("b2", Sheets("sheet1").Cells(Rows.Count, "b").End(xlUp)).SpecialCells(xlCellTypeVisible)

'clear Combobox2 list
ComboBox2.Clear

'Add visible items to Combobox2 List
For Each cell In frng
 ComboBox2.AddItem cell.Value
Next cell

'clear out applied filter
Sheets("sheet1").UsedRange.AutoFilter

'display Combobox2 dropdown
ComboBox2.DropDown

End Sub

hope this helps....

View attachment 49870
What would you define the variable frng as?
 
Upvote 0
What would you define the variable frng as?
VBA Code:
'focus filtered range
Set frng = Sheets("sheet1").Range("b2", Sheets("sheet1").Cells(Rows.Count, "b").End(xlUp)).SpecialCells(xlCellTypeVisible)

frng is set to focus filtered range
 
Upvote 0
VBA Code:
'focus filtered range
Set frng = Sheets("sheet1").Range("b2", Sheets("sheet1").Cells(Rows.Count, "b").End(xlUp)).SpecialCells(xlCellTypeVisible)

frng is set to focus filtered range
Maybe i have messed up somewhere?

Combobox1 is called ModelNo
Combobox2 is called PartNo

1635254379158.png
 
Upvote 0
try

VBA Code:
dim frng as range
Set frng = Sheets("sheet1").Range("b2", Sheets("sheet1").Cells(Rows.Count, "b").End(xlUp)).SpecialCells(xlCellTypeVisible)
 
Upvote 0

Forum statistics

Threads
1,215,686
Messages
6,126,202
Members
449,298
Latest member
Jest

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