I've been using "On Error Resume Next" for a while to 'step over' this error because I've been getting the correct results despite the error. However, now, I have a new issue to resolve, but I first need to fix the 380 error. I have notated the line that errors out and the On Error Resume Next is disabled.
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()
There are 125 columns in ListBox (.ColumnCount = 125
). For c = 0 To 125
tries to load 126 items. That's the root cause of run-time error. You can change 125
to 124
to fix it.
However, a more efficient approach involves using an array to collect all matched items and then updating the List
(or Column
) property all at once. (Thanks for @T.M.'s comment)
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 124 '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.Column = arrRes
Option Explicit
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
' modify as needed
Const COL_WIDE = "40;48;108;50;72;50;35;45;100;100"
Const COL_INDEX = "3,4,5,14,23,33,34,41,96,121" ' Col index which is showed in ListBox
Const START_ROW = 4 ' the frist data row
' ***
Application.ScreenUpdating = False 'Setting to 'false' speeds up the macro
Dim txtShopOrdNum As String: 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
Dim aIndex: aIndex = Split(COL_INDEX, ",")
Dim ColCnt As Long: ColCnt = UBound(aIndex) + 1
With lstMaster 'Need to clear the listbox first
.Clear
.ColumnCount = ColCnt
.ColumnWidths = COL_WIDE
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
Dim lastRow As Long: lastRow = Cells(Rows.Count, RN).End(xlUp).Row
Dim arrData: arrData = Range(Cells(START_ROW, 1), Cells(lastRow, "DQ"))
Dim iR As Long, arrRes()
For sat = LBound(arrData) To UBound(arrData)
deg1 = arrData(sat, RN)
If UCase(deg1) Like "*" & UCase(deg2) & "*" Then 'case insensitive AND searches the entire string
ReDim Preserve arrRes(ColCnt - 1, iR)
For c = 0 To ColCnt - 1 'column indices, total column
arrRes(c, iR) = arrData(sat, aIndex(c))
Next c
iR = iR + 1
End If
Next
lstMaster.Column = arrRes
' OR
' lstMaster.List = Application.Transpose(arrRes)
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