VBA linking listboxes

twildone

Board Regular
Joined
Jun 3, 2011
Messages
71
I have created an userform with two Listboxes having Multiple option.

The Listbox1, is populated with the Program_Number of column G of sheet Master.

The Listbox2, is populated with the Program_Name of column H of sheet Master.



What I am trying to do is if the Program_Name is selected from Listbox2, then the Program_Number in Listbox1 is automatically selected as well so that the Program_Number and Program_Name are unique I would also like to be able to enter new Program_Name and a corresponding Program_Number if I need to. How can I link these listboxes?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What I am trying to do is if the Program_Name is selected from Listbox2, then the Program_Number in Listbox1 is automatically selected

Try:

VBA Code:
Private Sub ListBox1_Click()
  ListBox2.ListIndex = ListBox1.ListIndex
End Sub

Private Sub ListBox2_Click()
  ListBox1.ListIndex = ListBox2.ListIndex
End Sub

Private Sub UserForm_Initialize()
  Dim lr As Long
  lr = Range("G" & Rows.Count).End(3).Row
  ListBox1.List = Range("G2:G" & lr).Value
  ListBox2.List = Range("H2:H" & lr).Value
End Sub

Ex:
1710460836974.png



I assume the above is for testing purposes. Since you can have the same thing with a single listbox and the listbox with 2 columns.

1710460923236.png


Try:
VBA Code:
Private Sub UserForm_Initialize()
  Dim lr As Long
  lr = Range("G" & Rows.Count).End(3).Row
  With ListBox1
    .ColumnCount = 2
    .List = Range("G2:H" & lr).Value
  End With
End Sub


Regards Dante Amor
😇
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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