I added the ListBox to the sheet (and not to the "UserForm"). I did this with the mouse. I clicked on the small hammer and wrench icon.
This ListBox seems to be easily referenced using code such as:
ListBox1.Clear
or
ListBox1.AddItem("An option")
However, I have three of these lists (names, conveniently, ListBox1, ListBox2 and ListBox3), and I want to write a function to populate them with array data, for example:
Call populate_listbox(ListBox2, designAreaArray)
If the first argument is the name of the list, then the second is the data.
But I do not know how to send "ListBox2" correctly or refer to it correctly.
For instance:
Dim controlName as string controlName = "ListBox1"
does not work even if I define the function as follows:
Sub populate_listbox(LB As ListBox, dataArray As Variant) Dim i As Integer: i = 0 For i = LBound(dataArray, 2) + 1 To UBound(dataArray, 2) ' Skip header row LB.AddItem (dataArray(index, i)) Next i End Sub
It is clear that this leads to an incorrect data type error. I tried to define "controlName" as a ListBox, but that didn't work either ...
Although, perhaps this is an invalid link to the listBox. I saw many ways to access the control object ...
MSForms.ListBox. ME.ListBox Forms.Controls. Worksheet.Shapes.
The list is on and nothing worked for me.