Listbox is set to Null somehow in excel-vba custom form

Context: I encode a custom form that will have some filters to start the procedure and fill the worksheet with a return value.

I am having problems with one of my filters. I was able to reproduce my problem in a shortened version. This filter should load data into the list based on the selected combobox option:

enter image description here

I have not renamed the personal components: UserForm1, ListBox1and ComboBox1.

My broken code (commented out):

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 the breakpoint, I see a problem. The parameter is ListBox1set to value Null, but I do not know how to get around it:

enter image description here

The error says:

Runtime Error "13": Type Mismatch

, ListBox1 - Null.

- ? ? .

+4
1

, , ListBox , VBA ( ). , MSForms ActiveX.

MSForms.ListBox, .

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

, list As Object, ( , ).

enter image description here

+5

Source: https://habr.com/ru/post/1681369/


All Articles