I have a workbook with three sheets:
-"DB" database sheet containing multiple tables (20 to be exact, named as "CityA", "CityB", etc.)
-"Threat Data" reference sheet containing a "City_Ref" table with the list of tables names in the "DB City ID" column and the unique city name assigned to each table in the "City" column (the city name is populated in a "City" column of each table in the database sheet).
-a dashboard sheet containing an interactive userform for which to populate the database tables.
In this userform, there are two comboboxes:
-a "CbxCity" combobox which lists all the city names from a "City" column in the "City_Ref" reference table
- a "CbxAsset" combobox which should automatically list all the values in the "Asset" column of the selected city table based on "CbxCity"
My issue lies in filling "CbxAsset" based on the selection in "CbxCity" ; how do I dynamically select a ListObject name based on a selection?
The code I am working with is:
Private Sub UserForm_Initialize()
Dim i As Range
For Each i In Worksheets("Threat Data").ListObjects("City_Ref").ListColumns("City").DataBodyRange
If i.Value <> "" Then
Me.CbxCity.AddItem i.Value
End If
Next i
End Sub
---------------------------------------------------------------------------------------------------------
Private Sub CbxCity_Change()
Dim r As Integer
Dim lo As ListObject
Dim i As Range
Dim oListObj As String
Me.CbxAsset.Clear
r = Me.CbxCity.Value
Set lo = Worksheets("Threat Data").ListObjects("City_Ref")
oListObj = lo.ListColumns("DB Table ID").DataBodyRange.Rows(r).Value
For Each i In Worksheets("DB").ListObjects(oListObj).ListColumns("City").DataBodyRange
If i.Offset(0, 1).Value <> "" And i.Value = CbxCity.Value Then
With Me.CbxAsset
.AddItem i.Offset(0, 1).Value
End With
End If
Next i
End Sub
In the CbxCity_Change() sub, I am not sure how to name the ListObject and my code currently gets an error at r = Me.CbxCity.Value
which is strange because that is showing the selected city name when I run the cursor over the bug.
Any help is greatly appreciated, please inform if my problem doe not make sense! Due to the size and confidential nature of my workbook, I am unable to post it here.
Bookmarks