Search code examples
excelvbaruntime-errorexcel-2013

Run-Time Errors 380 and 381 generated when searching for a word in a Listbox through a textbox on a Userform


I want to search for a word in a ListBox that has data drawn from a worksheet.

A video on YouTube Advance Multi column Search in Listbox with Single Textboxt, describes code to search in a ListBox for a keyword typed into a Textbox on the same Userform.

It works for a data set of 6 columns. When I change the code and corresponding control properties for a data set of 11 columns, the code throws

Run-Time Errors 380 and 381

in the two For... Next loops.

I am using Excel 2013 on Windows 10.

Private Sub TextBox2_AfterUpdate()

    With Me.ListBox2
        .Clear
        For ColHead = 1 To 11
            .AddItem
            .List(0, ColHead - 1) = Sheet2.Cells(1, ColHead).Value  '<<< 'Run-Time Error 380, Invalid Property Value
        Next ColHead
        
        ' If I comment out the For... Next loop above the code runs up to the next For... Next loop below _
      and there it throws up run-time error 381. I have marked it there with a comment.

        ListRow = 1
        If IsDate(Me.TextBox2) Then
            FindVal = CDate(Me.TextBox2)
        ElseIf IsNumeric(Me.TextBox2) Then FindVal = Val(Me.TextBox2)
        Else
            FindVal = "*" & Me.TextBox2 & "*"
        End If
        LastRow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
        For ShRow = 2 To LastRow
            FindRow = Application.WorksheetFunction.CountIf(Sheet2.Rows(ShRow).EntireRow, FindVal)
            If FindRow > 0 Then
                For ListCol = 1 To 11
                    .AddItem
                    .List(ListRow, ListCol - 1) = Sheet2.Cells(ShRow, ListCol).Value '<<< 'Run-Time Error 381, Invalid Property Value
                Next ListCol
                ListRow = ListRow + 1
            End If
        Next ShRow
    End With

End Sub

Solution

  • There is a limit of 10 columns if you use unbound data sources for a listbox:

    From the official documentation:

    Setting ColumnCount to 0 displays zero columns, and setting it to -1 displays all the available columns. For an unbound data source, there is a 10-column limit (0 to 9).

    Update: There are two ways to fill a listbox:
    The first is to assign a Range to the listbox (the property is called RowSouce). With that, you can have more than 10 columns, and you can have a header row. This is called "bound" data source (because the listbox data is bound to a range).
    The second is to fill the list manually (as you do), by calling AddItem for every row you want to display. As the data is not bound to anything, this is called "unbound", and for whatever reason there are a max of 10 columns supported (0..9) and you can't have a header row.

    The property BoundColumn defines which column is seen as "value". If you define 1 as boundColumn and check the value-property of the listbox (eg in a change event), the value of the first column of the selected row is used. This is true for bound and unbound lists.