Modify Search to use 2 textboxes instead of 1.

acombest

Board Regular
Joined
May 8, 2017
Messages
136
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?
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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top