Populating a dependent list box

charly1

Board Regular
Joined
Jul 18, 2023
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi All

I am trying to write some vba code for my user form, yet I keep on getting stuck. Any help would be very useful and greatly appreciated.

In my worksheet [worksheet1] I have a table [table1] with three columns. col1 = unique reference number. col2 = book name. col3 = book author.

In a user form I have two list boxes. list box 1 already contains two columns: LB1 col1 is a range of reference numbers retrieved from my table col1, and in LB1 col2, I have the matching book names.

I need a code that would loop through all rows in LB1 and populate LB2 with two separate columns. LB2 col1 should contain all the the reference numbers contained in LB1 col1 - in the same order [resulting in both list boxes containing an identical col1]. The second column of LB2, should then be populated with all of the matching book artist, as retrieved from the Table in column 3.

I hope I've explained myself clearly enough.

Thank you so much in advance for any help offered.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
the code below works well enough when the data source for the two list boxes are both coming from a single excel table.

VBA Code:
Private Sub CommandButton2_Click()
Dim searchrange As Range
Set searchrange = Worksheets("מפתח").ListObjects("Table1").ListColumns(1).DataBodyRange
Dim tableData As Range
Set tableData = Worksheets("מפתח").ListObjects("Table1").ListColumns(14).DataBodyRange
Dim Rr As Long
Dim Bb As Long
Bb = (ListBox1.ListCount - 1)
For Rr = 0 To Bb
With ListBox7
.ColumnCount = 2
.AddItem
.List(Rr, 0) = ListBox1.List(Rr, 0)
Dim c As String
c = WorksheetFunction.XLookup(ListBox1.List(Rr, 0), searchrange, tableData)
.List(Rr, 1) = c
End With
Next





End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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