填充列表框。 “运行时错误‘380’:无法设置 List 属性。属性值无效。”

问题描述 投票:0回答:1

尝试填充 ListBox 时,我总是在第 11 列上收到错误。

我在评估表中有 Excel 表格,其中包含从 B3 到 P24 的测试数据。前 4 列是文本数据,其余 11 列是 1 到 100 之间的数字 表的第一行包含第 4 列文本 - 在组合框中选择的评估者姓名。

我已经创建了用户窗体,并在其中放置了列表框。我还有一个带有评估者姓名的组合框。我只想用第 4 列中的记录填充列表框,其中第 4 列是在组合框中选择的评估员的姓名。

初始化 UserForm 后,我调用子 FilterPopulateListBox。

Private Sub FilterPopulateListBox()
    Dim PSelectedAssessor As String
    PSelectedAssessor = CmbAssessors.Value  ' Get the currently selected assessor's name

    Dim PWs As Worksheet
    Set PWs = ThisWorkbook.Sheets("Evaluations")
    Dim PLastRow As Long
    PLastRow = PWs.Cells(PWs.Rows.Count, "B").End(xlUp).Row

    Dim PData As Range
    Set PData = PWs.Range("B3:P" & PLastRow)

    ' Clear previous entries in the ListBox
    With lstEvaluations
        .Clear
        .ColumnCount = 15  ' Assuming there are 15 columns from B to P
        .ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50;50"

    
 Debug.Print lstEvaluations.ColumnCount  ' Should print 15
        
        ' Loop through each row in the range and add to ListBox if the fourth column matches
        Dim PRow As Range
        For Each PRow In PData.Rows
            If PRow.Cells(1, 4).Value = PSelectedAssessor Then  ' Check if fourth column matches the ComboBox
                Dim PRowArray() As Variant
                PRowArray = Application.Transpose(Application.Transpose(PRow.Value))
                .AddItem PRowArray(1)  ' Add first column value
                Dim i As Integer
                For i = 1 To UBound(PRowArray)  ' Add other columns
                    .List(.ListCount - 1, i - 1) = CStr(PRowArray(i))
                Next i
            End If
        Next PRow
    End With
End Sub

在这行代码中

.List(.ListCount - 1, i - 1) = CStr(PRowArray(i))

当 i=11 时我总是出错

当 i=11 时,PRowArray(i) 的值为 17,并且该行 .List(.ListCount - 1, i - 1) = PRowArray(i)

显示错误窗口“运行时错误‘380’:无法设置 List 属性。属性值无效。”

调试时,当我将光标指向 .List(.ListCount - 1, i - 1) 行的开头时,它会说。 “无法获取 List 属性。参数无效”但 PRowArray(i) 显示值 17 当 i 小于 11 时,它显示“Null”(在执行该行之前 - 当该行突出显示时)或当该行执行并且下一行突出显示时显示 PRowArray(i) 的值。

我已确保列表框有 15 列 我已将 PRowArray(i) 嵌套到 CStr(PRowArray(i)) 中来解决是否存在一些我不知道数据的问题 我已经调试了代码,看看代码为 i 1 到 11 读取了什么样的值。即使当 i=11 并且我指向 PRowArray(i) 时,它也能正确读取,该值是正确的 17

我阅读论坛 我问ChatGpt3.5

excel vba conditional-statements listbox population
1个回答
0
投票
  • 未绑定数据源有限制。最大列数为 10。

  • 尝试使用数组来填充超过 10 列的列表框。

Option Explicit
Private Sub FilterPopulateListBox()
    Dim PSelectedAssessor As String
    PSelectedAssessor = CmbAssessors.Value  ' Get the currently selected assessor's name
    Dim PWs As Worksheet
    Set PWs = ThisWorkbook.Sheets(1)
    Dim PLastRow As Long
    PLastRow = PWs.Cells(PWs.Rows.Count, "B").End(xlUp).Row
    Dim PData As Range
    Set PData = PWs.Range("B3:P" & PLastRow)
    Dim arrRes(), iR As Long, i As Integer
    ReDim arrRes(1 To 15, 1 To PData.Rows.Count)
    ' Clear previous entries in the ListBox
    With lstEvaluations
        .Clear
        .ColumnCount = 15  ' Assuming there are 15 columns from B to P
        .ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50;50"
        Debug.Print lstEvaluations.ColumnCount  ' Should print 15
        ' Loop through each row in the range and add to ListBox if the fourth column matches
        Dim PRow As Range
        For Each PRow In PData.Rows
            If PRow.Cells(1, 4).Value = PSelectedAssessor Then  ' Check if fourth column matches the ComboBox
                Dim PRowArray() As Variant
                PRowArray = Application.Transpose(Application.Transpose(PRow.Value))
                iR = iR + 1
                For i = 1 To UBound(PRowArray)  ' Add other columns
                    arrRes(i, iR) = CStr(PRowArray(i))
                Next i
            End If
        Next PRow
        ReDim Preserve arrRes(1 To 15, 1 To iR)
        .Column = arrRes
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.