操纵11列

时间:2015-12-04 13:18:36

标签: excel vba excel-vba listbox userform

我有一个包含11列的列表框。当我尝试将数据添加到其中一列时,我收到错误。

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

我不明白为什么会发生这种情况,用户表单上的列表框的 ColumnCount为11

错误我得到了:

  

"运行时错误380:无法设置Column属性。无效的属性值。"

所选单元格的值为" Group 16"。


更多信息:

代码:

'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

1 个答案:

答案 0 :(得分:2)

这就是我的意思(您可以通过将工作表数据加载到数组中来开始和处理它,而不是经常调整数组大小来提高性能,但这会分散注意力!):

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