Home » excel » excel – Manipulate a listbox of 11 columns

excel – Manipulate a listbox of 11 columns

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve got a listbox of 11 columns. When I try to add data to one of the columns, I get an error.

ListBox1.Column(10, j) = shtG.Cells(k, 13)

I don’t understand why this happens, the listbox on the userform has a ColumnCount of 11.

The error I’m getting:

“Run-time error 380: Unable to set Column property. Invalid property value.”

The value of the selected cell is “Group 16”.

More info:

Code:

'adding this doesn't help
ListBox1.Clear
ListBox1.ColumnCount = 20

    While shtG.Cells(k, 1) <> ""
        If 'some long working condition Then

            frmTP.ListBox1.AddItem (shtG.Cells(k, kolID))
            frmTP.ListBox1.Column(1, j) = shtG.Cells(k, kolVnm) & strSpace & shtG.Cells(k, kolTV) & strSpace & shtG.Cells(k, kolAnm)
            frmTP.ListBox1.Column(2, j) = shtG.Cells(k, 5)
            frmTP.ListBox1.Column(3, j) = shtG.Cells(k, 6)
            frmTP.ListBox1.Column(4, j) = shtG.Cells(k, 7)
            frmTP.ListBox1.Column(5, j) = shtG.Cells(k, 8)
            frmTP.ListBox1.Column(6, j) = shtG.Cells(k, 9)
            frmTP.ListBox1.Column(7, j) = shtG.Cells(k, 10)
            frmTP.ListBox1.Column(8, j) = shtG.Cells(k, 11)
            frmTP.ListBox1.Column(9, j) = shtG.Cells(k, 12)
            frmTP.ListBox1.Column(10, j) = shtG.Cells(k, 13)
            j = j + 1
        End If
        k = k + 1
    Wend
How to&Answers:

This is the sort of thing I mean (you could improve performance by loading the sheet data into an array to begin and processing that, and not resizing the array so often, but it would distract from the key idea here!):

Dim vData()
j = 0
While shtG.Cells(k, 1) <> ""
    If 'some long working condition Then
        ReDim Preserve vData(0 To 10, 0 To j)
        vData(0, j) = shtG.Cells(k, kolID).Value
        vData(1, j) = shtG.Cells(k, kolVnm) & strSpace & shtG.Cells(k, kolTV) & strSpace & shtG.Cells(k, kolAnm)
        vData(2, j) = shtG.Cells(k, 5)
        vData(3, j) = shtG.Cells(k, 6)
        vData(4, j) = shtG.Cells(k, 7)
        vData(5, j) = shtG.Cells(k, 8)
        vData(6, j) = shtG.Cells(k, 9)
        vData(7, j) = shtG.Cells(k, 10)
        vData(8, j) = shtG.Cells(k, 11)
        vData(9, j) = shtG.Cells(k, 12)
        vData(10, j) = shtG.Cells(k, 13)
        j = j + 1
    End If
Wend
frmTP.ListBox1.Column = vData