我需要搜索项目,找到匹配项后,将它们放入用户表单中。我可以在按下搜索按钮时进行搜索,但在再次按下相同按钮时无法搜索下一个匹配项( CommandButton4 )。
我想通过按下按钮找到所有匹配项,直到找不到任何内容。这是我的代码:
Private Sub CommandButton4_Click()
Dim ws As Worksheet
Dim FindString As String
Dim Rng As Range
Set ws = ThisWorkbook.Worksheets("BİLGİLER")
FindString = TextBox2.Value
If Trim(FindString) <> "" Then
Set Rng = ws.Cells.Find( _
What:=FindString, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
TextBox1.Value = ActiveCell.Offset(0, -1).Value
TextBox2.Value = ActiveCell.Offset(0, 0).Value
ComboBox1.Value = ActiveCell.Offset(0, 1).Value
TextBox3.Value = ActiveCell.Offset(0, 2).Value
TextBox4.Value = ActiveCell.Offset(0, 3).Value
TextBox5.Value = ActiveCell.Offset(0, 4).Value
TextBox6.Value = ActiveCell.Offset(0, 5).Value
TextBox7.Value = ActiveCell.Offset(0, 6).Value
ComboBox2.Value = ActiveCell.Offset(0, 7).Value
TextBox8.Value = ActiveCell.Offset(0, 8).Value
TextBox9.Value = ActiveCell.Offset(0, 9).Value
TextBox10.Value = ActiveCell.Offset(0, 15).Value
TextBox11.Value = ActiveCell.Offset(0, 16).Value
TextBox12.Value = ActiveCell.Offset(0, 17).Value
TextBox13.Value = ActiveCell.Offset(0, 18).Value
TextBox14.Value = ActiveCell.Offset(0, 19).Value
TextBox15.Value = ActiveCell.Offset(0, 20).Value
TextBox16.Value = ActiveCell.Offset(0, 21).Value
TextBox17.Value = ActiveCell.Offset(0, 22).Value
ComboBox3.Value = ActiveCell.Offset(0, 23).Value
TextBox18.Value = ActiveCell.Offset(0, 24).Value
TextBox19.Value = ActiveCell.Offset(0, 25).Value
TextBox20.Value = ActiveCell.Offset(0, 10).Value
TextBox21.Value = ActiveCell.Offset(0, 11).Value
TextBox22.Value = ActiveCell.Offset(0, 12).Value
TextBox23.Value = ActiveCell.Offset(0, 13).Value
TextBox24.Value = ActiveCell.Offset(0, 14).Value
ComboBox4.Value = ActiveCell.Offset(0, 26).Value
ComboBox5.Value = ActiveCell.Offset(0, 27).Value
ActiveCell.EntireRow.Select
Else
MsgBox "Nothing found"
End If
End If
End Sub
我知道它不会那么难,但我的大脑已停止工作了。 :)我已经做了很多搜索,我现在甚至都想不起......先谢谢。
答案 0 :(得分:0)
请在代码末尾添加以下循环以查找所有其他匹配项:
dim sFirstMatchAddress as string
sFirstMatchAddress = Rng.address
With ws.Cells
Do
Set rng = .FindNext(rng)
If sFirstMatchAddress = rng.Address Then Exit Do
'Fill the text boxes*
Loop
end with
此外,我想我们无法预测您需要处理所有匹配项的文本框。您可能需要考虑如何动态地删除文本框以及匹配数量。
其次,您不必使用
Application.Goto Rng, True
和
TextBox1.Value = ActiveCell.Offset(0, -1).Value
相反,只放:
TextBox1.Value = Rng.Offset(0, -1).Value
答案 1 :(得分:0)
工作表(“BILGILER”)必须是ActiveSheet,因为您正在选择单元格。
Private Sub CommandButton4_Click()
Dim FindString As String
Dim Rng As Range, SearchRange As Range
ThisWorkbook.Worksheets("BILGILER").Select
Set SearchRange = Intersect(Columns("C"), ActiveSheet.UsedRange)
FindString = TextBox2.Value
If Trim(FindString) <> "" Then
Set Rng = SearchRange.Find( _
What:=FindString, _
After:=Cells(ActiveCell.Row, "C"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
TextBox1.Value = ActiveCell.Offset(0, -1).Value
TextBox2.Value = ActiveCell.Offset(0, 0).Value
ComboBox1.Value = ActiveCell.Offset(0, 1).Value
TextBox3.Value = ActiveCell.Offset(0, 2).Value
TextBox4.Value = ActiveCell.Offset(0, 3).Value
TextBox5.Value = ActiveCell.Offset(0, 4).Value
TextBox6.Value = ActiveCell.Offset(0, 5).Value
TextBox7.Value = ActiveCell.Offset(0, 6).Value
ComboBox2.Value = ActiveCell.Offset(0, 7).Value
TextBox8.Value = ActiveCell.Offset(0, 8).Value
TextBox9.Value = ActiveCell.Offset(0, 9).Value
TextBox10.Value = ActiveCell.Offset(0, 15).Value
TextBox11.Value = ActiveCell.Offset(0, 16).Value
TextBox12.Value = ActiveCell.Offset(0, 17).Value
TextBox13.Value = ActiveCell.Offset(0, 18).Value
TextBox14.Value = ActiveCell.Offset(0, 19).Value
TextBox15.Value = ActiveCell.Offset(0, 20).Value
TextBox16.Value = ActiveCell.Offset(0, 21).Value
TextBox17.Value = ActiveCell.Offset(0, 22).Value
ComboBox3.Value = ActiveCell.Offset(0, 23).Value
TextBox18.Value = ActiveCell.Offset(0, 24).Value
TextBox19.Value = ActiveCell.Offset(0, 25).Value
TextBox20.Value = ActiveCell.Offset(0, 10).Value
TextBox21.Value = ActiveCell.Offset(0, 11).Value
TextBox22.Value = ActiveCell.Offset(0, 12).Value
TextBox23.Value = ActiveCell.Offset(0, 13).Value
TextBox24.Value = ActiveCell.Offset(0, 14).Value
ComboBox4.Value = ActiveCell.Offset(0, 26).Value
ComboBox5.Value = ActiveCell.Offset(0, 27).Value
ActiveCell.EntireRow.Select
Else
MsgBox "Nothing found"
End If
End If
End Sub
答案 2 :(得分:0)
试试这个。它只将数据读取到用户表单。
Option Explicit
Private rngLastFound As Range
Private Sub CommandButton4_Click()
Dim ws As Worksheet
Dim FindString As String
Dim Rng As Range, SearchRange As Range
Set ws = ThisWorkbook.Worksheets("BILGILER")
If rngLastFound Is Nothing Then Set rngLastFound = ws.Range("C1")
Set SearchRange = Intersect(Columns("C"), ws.UsedRange)
FindString = TextBox2.Value
If Trim(FindString) <> vbNullString Then
Set Rng = SearchRange.Find( _
What:=FindString, _
After:=ws.Cells(rngLastFound.Row, SearchRange.Column), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Rng Is Nothing Then
Set rngLastFound = Rng
TextBox1.Value = Rng.Offset(0, -1).Value
TextBox2.Value = Rng.Offset(0, 0).Value
ComboBox1.Value = Rng.Offset(0, 1).Value
TextBox3.Value = Rng.Offset(0, 2).Value
TextBox4.Value = Rng.Offset(0, 3).Value
TextBox5.Value = Rng.Offset(0, 4).Value
TextBox6.Value = Rng.Offset(0, 5).Value
TextBox7.Value = Rng.Offset(0, 6).Value
ComboBox2.Value = Rng.Offset(0, 7).Value
TextBox8.Value = Rng.Offset(0, 8).Value
TextBox9.Value = Rng.Offset(0, 9).Value
TextBox10.Value = Rng.Offset(0, 15).Value
TextBox11.Value = Rng.Offset(0, 16).Value
TextBox12.Value = Rng.Offset(0, 17).Value
TextBox13.Value = Rng.Offset(0, 18).Value
TextBox14.Value = Rng.Offset(0, 19).Value
TextBox15.Value = Rng.Offset(0, 20).Value
TextBox16.Value = Rng.Offset(0, 21).Value
TextBox17.Value = Rng.Offset(0, 22).Value
ComboBox3.Value = Rng.Offset(0, 23).Value
TextBox18.Value = Rng.Offset(0, 24).Value
TextBox19.Value = Rng.Offset(0, 25).Value
TextBox20.Value = Rng.Offset(0, 10).Value
TextBox21.Value = Rng.Offset(0, 11).Value
TextBox22.Value = Rng.Offset(0, 12).Value
TextBox23.Value = Rng.Offset(0, 13).Value
TextBox24.Value = Rng.Offset(0, 14).Value
ComboBox4.Value = Rng.Offset(0, 26).Value
ComboBox5.Value = Rng.Offset(0, 27).Value
Else
MsgBox "Nothing found"
End If
End If
End Sub