我有一个包含 9 列的列表框(lstMaster);一个组合框 (cboSearchItem),其值表示列标题和一个文本框 (txtSearch)。
用户从 cboSearchItem 中选择一个值,并在 txtSearch 中输入一个值。例如,用户选择“PO”并在txtSearch中输入“61”。结果是正确的。任何包含“61”的 PO 都会填充 lstMaster。
但是,即使它有效,我也会收到错误:运行时错误'-2147352571 (80020005)':无法设置List属性。类型不匹配
请参阅下面的错误行:lstMaster.List(s, c) = Cells(sat, c + 1)
我非常感谢一些帮助。
**EDITED**
'***Search Multiple Orders Button***
Private Sub cmbSearchlstMaster_Click()
Dim sat As Long
Dim s As Long
Dim c As Integer
Dim deg1 As String
Dim deg2 As String
Dim RN As Integer
txtShopOrdNum = "" 'clear txtShopOrdNum
Sheets("Master").Activate
Application.ScreenUpdating = False 'Setting to 'false' speeds up the macro
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 = 120
.ColumnWidths = "0;0;0;100;120;0;0;0;0;0;0;0;0;0;75;0;0;0;0;0;0;0;0;110;0;0;0;0;0;0;0;0;0;50;40;0;0;0;0;0;0;60;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;100;0;0;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 'column number
Case "Suffix"
RN = 4
Case "Proposal"
RN = 15
Case "PO"
RN = 24
Case "SO"
RN = 34
Case "Quote"
RN = 35
Case "Transfer Order"
RN = 42
Case "Customer Name"
RN = 93
Case "End User Name"
RN = 116
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 119 'column indices, total columns
'On Error Resume Next 'disabled this, no longer getting Run-Time Error on 2147352571 on the next line anymore...not sure how I fixed it.
lstMaster.List(s, c) = Cells(sat, c + 1)
'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 (last) 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 'cmbSearchlstMaster
编辑后的代码现在可以工作了,老实说我不明白为什么。显然,我一定是编辑了另一个影响此代码的过程?无论如何,我希望这对其他人有帮助。