You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA填充ListBox触发Run-time error '380'错误求助

解决ListBox填充第11列时的Run-time Error '380'问题

问题分析

你遇到的Run-time Error '380'通常在ListBox控件属性赋值不符合预期时触发。虽然你已经确认了列数和数据有效性,但逐个列赋值的方式容易引发控件内部状态的隐性异常——你的代码先通过AddItem添加第一列,再循环覆盖所有列,这种重复操作可能在处理到第11列(索引10)时触发控件的属性校验错误。

解决方案

推荐使用批量数组赋值的方式填充ListBox,既避免循环赋值的潜在问题,又能提升代码效率。以下是修改后的代码:

Private Sub FilterPopulateListBox()
    Dim PSelectedAssessor As String
    PSelectedAssessor = CmbAssessors.Value

    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)

    ' 收集符合条件的行数据
    Dim filteredRows As Collection
    Set filteredRows = New Collection
    
    Dim PRow As Range
    For Each PRow In PData.Rows
        If PRow.Cells(1, 4).Value = PSelectedAssessor Then
            filteredRows.Add PRow.Value ' 将整行数据加入集合
        End If
    Next PRow

    ' 填充ListBox
    With lstEvaluations
        .Clear
        .ColumnCount = 15
        .ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50;50"

        If filteredRows.Count > 0 Then
            ' 将集合转换为二维数组,一次性赋值给ListBox
            Dim outputArray() As Variant
            ReDim outputArray(1 To filteredRows.Count, 1 To 15)
            
            Dim i As Long
            For i = 1 To filteredRows.Count
                outputArray(i, 1 To 15) = filteredRows(i)
            Next i
            
            .List = outputArray
        End If
    End With
End Sub

关键改进点

  1. 批量收集数据:先用Collection存储所有符合条件的行,避免边循环边操作ListBox控件,减少控件交互次数
  2. 一次性赋值:将收集到的数据转换为二维数组后,直接赋值给ListBox.List,彻底规避逐个列赋值的索引风险
  3. 代码更简洁高效:逻辑清晰,降低触发控件异常的概率

额外排查建议

如果问题仍未解决,可以检查以下几点:

  • 确认lstEvaluations控件在设计视图中的ColumnCount属性没有被设置为小于15的值
  • 检查Evaluations工作表中对应第11列的原数据(O列)是否存在特殊格式或不可见字符
  • 尝试重新创建ListBox控件,排除控件本身损坏的可能性

内容的提问来源于stack exchange,提问作者Radovan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:12:14