Private Sub CommandButton10_Click()
'listbox column headers
Me.ListBox1.AddItem
For A = 1 To 7
Me.ListBox1.List(0, A - 1) = Sheet2.Cells(1, A)
Next A
Me.ListBox1.Selected(0) = True
'Populating listbox from search
Dim i As Long
For i = 2 To Sheet2.Range("A100000").End(xlUp).Offset(1, 0).Row
For j = 1 To 7
H = Application.WorksheetFunction.CountIf(Sheet2.Range("A" & 2, "G" & i), _
Sheet2.Cells(i, j))
If H = 1 And LCase(Sheet2.Cells(i, j)) = LCase(Me.TextBox2) Or H = 1 And _
Sheet2.Cells(i, j) = Val(Me.TextBox2) Then
Me.ListBox1.AddItem
For x = 1 To 7
Me.ListBox1.List(ListBox1.ListCount - 1, x - 1) = Sheet2.Cells(i, x)
Next x
End If
Next j
Next i
'Count the listbox rows when populated
With Me.ListBox1
For x = 0 To .ListCount - 1
TextBox3 = x
Next x
End With
End Sub
大家好,我需要帮助,谁能告诉我为什么我的代码无法搜索工作表中与文本相关的所有内容,例如工作表中有2个相同的项目,但序列号不同,但是当我搜索该项目时,仅显示1条记录。谢谢。
答案 0 :(得分:0)
我认为您应该更改:
H = Application.WorksheetFunction.CountIf(Sheet2.Range("A" & 2, "G" & i), Sheet2.Cells(i, j))
收件人:
H = Application.WorksheetFunction.CountIf(Sheet2.Range("A" & i, "G" & i), Sheet2.Cells(i, j))
这样:
正在逐行搜索匹配项
以下检查If H = 1 And LCase(Sheet2.Cells(i, j)) ...
不排除同一项目的任何可能的多次出现(如果从第2行开始向下计数,则将返回H> 1)
注意:这样一来,您将错过同一行中多次出现的商品的所有可能匹配项