VBA 用户窗体列表框 - 运行时错误 380。无法设置列表属性。无效的属性值

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

我已经使用“错误时继续下一步”一段时间来“跳过”此错误,因为尽管存在错误,我仍然得到了正确的结果。但是,现在我有一个新问题需要解决,但我首先需要修复 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()
excel vba listbox runtime-error userform
1个回答
0
投票
  • 使用数组收集所有匹配的项,然后一次性更新
    List
    属性。
    Dim RowCnt As Long, iR As Long, arrRes
    RowCnt = Cells(Rows.Count, RN).End(xlUp).Row
    For sat = 4 To RowCnt '4 = first row of databody
        deg1 = Cells(sat, RN)
        If UCase(deg1) Like "*" & UCase(deg2) & "*" Then 'case insensitive AND searches the entire string
            ReDim Preserve arrRes(125, iR)
            lstMaster.AddItem
            For c = 0 To 125                     'column indices, total columns
                arrRes(c, iR) = Cells(sat, c + 1)
            Next c
            iR = iR + 1
            s = s + 1                             'This MUST follow "Next c", this increments s for each new record added
        End If
    Next
    lstMaster.List = Application.Transpose(arrRes)
© www.soinside.com 2019 - 2024. All rights reserved.