VBA - Use a combobox to generate list box items

JohnSmith9945

New Member
Joined
Sep 30, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hey! im really struggling to understand how, based on a combobox selection i can pull records into a listbox. Ive tried the below code based on some videos but it still doesnt quite make sense/isnt doing what i need it too.

i just want to to be able to select an ID in the combo box, press apply filter and have that search my table and display all records that have that ID in their row, in the list box.

Any help would be much appreciated!

VBA Code:
Option explicit 
Dim my data As range 

Private sub applyfilternav_click ()
Set mydata  = Worksheets ("dataID").range ("A1").CurrentRegion 
Me.Combobox1.list =mydata.Offset(1).Value 
Me.Listbox1.ColumnCount = 13 
Me.Listbox1.Columnheads = true 
Me.Listbox1.ColumnCount = "10,10,10,10,10,10,10,10,10,10,10,10,10"

Dim mysearchrng as range 
Dim myfindrange as range 
Dim myvaltofind as variant 

With Worksheets("DataIssues") 
myvaltofind =combobox1.value 
set mysearchrng =.Columns("C)
end with 

set myfindrng  = mysearchrng.find(what:=myvaltofind, _ 
Lookat:=xlwhole, _ 
Searchorder: = xlByrows, _ 
Searchdirection: =xlnext, _ 
matchcase: =false)

Listbox1.additem 
with listbox1
.list (.listcount -1, 2)= myfindrng.value
.list (.listcount -1, 0)= myfindrng.offset (0,1).value
.list (.listcount -1, 1)= myfindrng.offset (0,2).value
.list (.listcount -1, 3)= myfindrng.offset (0,4).value

private sub userform_intialize() 
combobox1.list = worksheets("detailsID").range("b2:100").value
end sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
for i = 0 to cboBox.listcount - 1      'in vb, counts begin with zero
     lstBox.AddItem  = cboBox.itemdata(i)
next
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,289
Members
449,149
Latest member
mwdbActuary

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