Currently this code searches sheet 1 for a row in A2:M100 that matches a string entered in textbox 1 and displays the results on sheet 2.
I would like to modify it to add a 2nd textbox and have it search for a row that has a match in both textbox1 and textbox 2 and display the results and sheet 2.
Any ideas on how I can accomplish this?
I would like to modify it to add a 2nd textbox and have it search for a row that has a match in both textbox1 and textbox 2 and display the results and sheet 2.
Any ideas on how I can accomplish this?
Code:
Private Sub CommandButton1_Click()
Dim FirstAddress As String
Dim MyArr As Variant
Dim Rng As Range
Dim I As Long, n As Long
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
Application.ScreenUpdating = False
'Search for a Value Or Values in a range
MyArr = Array(TextBox1.text)
[COLOR=#0000ff] n = 1[/COLOR]
'Search Column or range
With sh1.Range("A1:M100")
'clear sheet of old search results
sh2.Range("A:M").ClearContents
For I = LBound(MyArr) To UBound(MyArr)
'If you want to find a part of the rng.value then use xlPart
'if you use LookIn:=xlValues it will also work with a
'formula cell that evaluates to "ron"
Set Rng = .Find(What:=MyArr(I), _
After:=.Cells(.Cells.count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
If sh1.Cells(Rng.row, "Z") <> "x" Then
n = n + 1
sh2.Range(sh2.Cells(n, "A"), sh2.Cells(n, "M")).Value = sh1.Range(sh1.Cells(Rng.row, "A"), sh1.Cells(Rng.row, "M")).Value
'using col Z as helper column to mark the row so it won't be copied twice
sh1.Cells(Rng.row, "Z") = "x"
End If
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
End If
Next I
End With
'clear col Z
sh1.Columns("Z").ClearContents
Application.ScreenUpdating = True
End Sub