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

Viewed 3306

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.

1 Answers
Related