VBA - Dynamic Listbox from Variable ComboBox

MrMisster

New Member
Joined
Feb 21, 2020
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am trying to add a form, that allows the user to first create a team, then add members to that team. Once the team has been inserted it populates a ComboBox, I would like to be able to populate a list box with the Team Members based on the team selected in the ComboBox. I have tried a good amount of ways, getting very close, but cannot seem to set the Table Column variable correctly. Any help is appreciated.

VBA Code:
Private Sub ComboBox1_Change()

Dim tbl As ListObject
Dim cell As Range
Set sh = ThisWorkbook.Sheets("Validation")
Dim n As Integer
Dim i As Integer

n = Application.WorksheetFunction.Match(Me.ComboBox1.Value, sh.Range("1:1"), 0)

'Store Table Object to a variable
Set tbl = Sheets("Validation").ListObjects("Teams")

'Clear Listbox each time a change is initialised
ListBox1.Clear

'Load Combobox with column 1 data teams
Me.ComboBox1.List = Sheets("Validation").ListObjects("Teams").ListColumns(1).DataBodyRange.Value

'Load List Box
  For Each cell In tbl.DataBodyRange.Columns(n).Cells
    ListBox1.AddItem cell.Value
  Next cell

End Sub

Screenshots of form and table;

OnLoad.PNG
Team Selected.PNG
Table.PNG
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
VBA Code:
Private Sub ComboBox1_Click()
   Dim tbl As ListObject

   Set tbl = ThisWorkbook.Sheets("Validation").ListObjects("Teams")
   Me.ListBox1.Clear
   Me.ListBox1.List = tbl.ListColumns(Me.ComboBox1.Value).DataBodyRange.Value
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Private Sub ComboBox1_Click()
   Dim tbl As ListObject

   Set tbl = ThisWorkbook.Sheets("Validation").ListObjects("Teams")
   Me.ListBox1.Clear
   Me.ListBox1.List = tbl.ListColumns(Me.ComboBox1.Value).DataBodyRange.Value
End Sub

Wow, I want to punch myself in the face. I went with something very similar early on and could not get it to work so I have been sat for 2 days going around the houses with different methods. The .ListColumns() was where I fell down massively and I actually tried combox1.value without the reference to the form and it failed, obviously.

Thanks Fluff, appreciated!
 
Upvote 0
You're welcome & thanks for the feedback.

As you were using the Change event, if you manually type in a value (say Manowar), as soon as you type the "M" the code will be looking for a column called "M" & will therefore fail.
 
Upvote 0
Not a problem,

Did not think of that, makes much more sense to use the click event instead.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

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