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

VBA用户窗体ListBox运行时错误380:无法设置List属性,无效值

修复VBA运行时错误380(无效属性值)

我之前一直用On Error Resume Next跳过错误,虽然报错但结果是对的。现在必须先修复运行时错误380才能解决新问题,已经禁用了On Error Resume Next并标出了报错行,相关代码如下:

Private Sub imgSearchlstMaster_Click() 'Search multiple orders button
    Dim sat    As Long
    Dim s      As Long
    Dim c      As Integer
    Dim deg1   As String
    Dim deg2   As String
    Dim RN     As Integer
    Application.ScreenUpdating = False 'Setting to 'false' speeds up the macro
    txtShopOrdNum = ""                            'clear txtShopOrdNum, v13

    Sheets("Master").Activate
    If Me.txtSearch.Value = "" Then               'Condition if the textbox is blank
        MsgBox "Please enter a search value.", vbOKOnly + vbExclamation, "Search" 'vbOKOnly shows only the OK button, vbExclamation shows exclamation point icon
        txtSearch.SetFocus
        Exit Sub
    End If
    If cboSearchItem.Value = "" Then              ' Condition if combobox is blank
        MsgBox "Please select search criteria.", vbOKOnly + vbExclamation, ""
        cboSearchItem.SetFocus
        Exit Sub
    End If
    
    With lstMaster                                'Need to clear the listbox first
        .Clear
        .ColumnCount = 125
        .ColumnWidths = "0;0;40;48;108;0;0;0;0;0;0;0;0;50;0;0;0;0;0;0;0;0;72;0;0;0;0;0;0;0;0;0;50;35;0;0;0;0;0;0;45;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;100;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;100;0;0;"
        'Must include the total ColumnCount and all ColumnWidths for the code to work. ColumnWidths = 0 do not appear in the listbox.
    End With
    
    Call Main                                     'Progress bar
    deg2 = txtSearch.Value
    
    Select Case cboSearchItem.Value
        Case "Shop Order"
            RN = 5  'E                          'column number
        Case "Suffix"
            RN = 4  'D
        Case "Proposal"
            RN = 14 'O
        Case "PO"
            RN = 23 'X
        Case "SO"
            RN = 33 'AH
        Case "Quote"
            RN = 34 'AI
        Case "Transfer Order"
            RN = 41 'AP
        Case "Customer Nickname"
            RN = 96 'CR
        Case "End User Nickname"
            RN = 121 'DQ
    End Select
    
    For sat = 4 To Cells(Rows.Count, RN).End(xlUp).Row '4 = first row of databody
        deg1 = Cells(sat, RN)
        If UCase(deg1) Like "*" & UCase(deg2) & "*" Then 'case insensitive AND searches the entire string
            lstMaster.AddItem
            For c = 0 To 125                     'column indices, total columns
            'On Error Resume Next 'added this to get past Run-Time error 308
                lstMaster.List(s, c) = Cells(sat, c + 1) 'Run-Time error 308. Could not set the List property. Invalid property value
                's = count of txtSearch.Value results
                'c = (total number of columns)
                'c + 1 = (total number of columns + 1)
                'sat = first blank row
                'deg1 = lower bound value in column RN (sorting will change this)
                'deg2 = txtSearch.Value
                'RN = column number of cboSearchItem.Value
            Next c
            s = s + 1                             'This MUST follow "Next c", this increments s for each new record added
        End If
    Next
    Application.ScreenUpdating = True
    
    lblTotalSearchResults = lstMaster.ListCount
    lblTotalOrders = Range("MASTER").Rows.Count
    
    'Debug.Print s, c, c + 1, sat, deg1, deg2, RN, lblTotalSearchResults, lblTotalOrders
    
End Sub  'imgSearchlstMaster_Click()

错误原因分析

报错行lstMaster.List(s, c) = Cells(sat, c + 1)触发运行时错误380,核心原因是:

  • 列表框lstMaster的ColumnCount设为125,意味着列索引范围是0到124(共125列)
  • 循环For c = 0 To 125会尝试访问索引125的列,超出了列表框的有效列范围,导致"无效属性值"错误

修复方案

1. 修正循环列索引范围

把列循环的上限从125改为124,匹配列表框的列数:

For c = 0 To 124 ' 原代码是To 125,改为124
    lstMaster.List(s, c) = Cells(sat, c + 1)
Next c

2. 显式初始化计数器s

在变量声明后添加s = 0,避免默认值可能引发的索引混乱:

Dim RN     As Integer
s = 0 ' 显式初始化行计数器
Application.ScreenUpdating = False

3. 可选优化:用数组批量赋值提升效率

直接循环赋值列表框效率较低,可先把符合条件的数据存入二维数组,再一次性赋值给列表框,同时避免索引错误:

' 替换原有的For sat循环部分
Dim arrData As Variant
Dim resultCount As Long
Dim i As Long

' 先统计符合条件的行数
resultCount = 0
For sat = 4 To Cells(Rows.Count, RN).End(xlUp).Row
    deg1 = Cells(sat, RN)
    If UCase(deg1) Like "*" & UCase(deg2) & "*" Then
        resultCount = resultCount + 1
    End If
Next sat

' 初始化数组
If resultCount > 0 Then
    ReDim arrData(1 To resultCount, 1 To 125)
    i = 0
    For sat = 4 To Cells(Rows.Count, RN).End(xlUp).Row
        deg1 = Cells(sat, RN)
        If UCase(deg1) Like "*" & UCase(deg2) & "*" Then
            i = i + 1
            For c = 1 To 125
                arrData(i, c) = Cells(sat, c).Value
            Next c
        End If
    Next sat
    ' 批量赋值给列表框
    lstMaster.List = arrData
End If

' 替换原有的s计数逻辑,直接用resultCount
lblTotalSearchResults = resultCount

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:25:29