Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listbox is being set to Null somehow in excel-vba user form

Context: I am coding a user form which will have some filters to run a procedure and fill a worksheet with the return value.

I am having trouble with one of my filters. I was able to reproduce my issue in a reduced version. This filter should load data into a listbox based on the selected combobox option:

enter image description here

I didn't rename anything, the components are: UserForm1, ListBox1 and ComboBox1.

My broken code (commented):

Option Explicit

'sub that fill data in the list box columns
Sub loadList(list As ListBox, id As Integer)
    list.Clear
    If (id > 0) Then
        list.AddItem
        list.Column(0, 0) = "Item 1"
        list.AddItem
        list.Column(0, 1) = "Item 2"
    End If
End Sub

'event that will trigger the loadList sub
Private Sub ComboBox1_Change()
    Dim id As Integer
    id = ComboBox1.ListIndex

    loadList ListBox1, id
End Sub

'the combo options is auto initialized
Private Sub UserForm_Initialize()
    ComboBox1.AddItem
    ComboBox1.Column(0, 0) = "Option 1"
    ComboBox1.AddItem
    ComboBox1.Column(0, 1) = "Option 2"
End Sub

When I set a brekpoint I can see the problem. The ListBox1 is being set to Null, but I don't know how to work around it:

enter image description here

The error says:

Run-time error '13': Type mismatch

But it is obvious because the ListBox1 is being set to Null somehow.

Have anyone experienced this behaviour before? How to work around it? Thanks in advance.

like image 587
Washington Guedes Avatar asked Jul 13 '17 13:07

Washington Guedes


People also ask

How do you create a list box in Excel UserForm?

A list box in Excel is a list assigned to a variable that can be used to select multiple items. On a UserForm, a list box can be inserted by selecting the list box option. While creating a list box in Excel, the details in “ListFillRange,” “MultiSelect,” and “ListStyle” properties have to be entered.

How do I add data to a listbox in Excel VBA?

There are 3 ways to add items to the VBA Listbox: One at a time using the AddItem property. Adding an array/range using the List property. Adding a Range using the RowSource property.

How do you edit a listbox in Excel?

Edit a drop-down list with items that have been entered manually. On the worksheet where you applied the drop-down list, select a cell that has the drop-down list. Go to Data > Data Validation. On the Settings tab, click in the Source box, and then change your list items as needed.


1 Answers

Strange as it looks, there are two classes named ListBox from different libraries, and VBA confuses them (the same applies to all controls actually). It depends on whether you are using the MSForms controls or the ActiveX controls.

In your case, you should disambiguate with MSForms.ListBox which is the actual type of your listbox.

Sub loadList(list As MSForms.ListBox, id As Integer)
'                    ^^^^^^^

To avoid such doubts, you can also use list As Object, (would make your sub handle both types as long as you use only the common methods).

enter image description here

like image 138
A.S.H Avatar answered Oct 11 '22 07:10

A.S.H