VBA - 搜索未在列表框中显示多条记录
VBA - Search not showing multiple records in Listbox
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 条记录。谢谢
我认为你应该改变:
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 行向下计算,则 return H>1)
注意:这样您将错过在同一行中多次出现的项目的所有可能匹配项
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 条记录。谢谢
我认为你应该改变:
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 行向下计算,则 return H>1)
注意:这样您将错过在同一行中多次出现的项目的所有可能匹配项