Home » excel » Excel VBA search two different columns for a result to then paste

Excel VBA search two different columns for a result to then paste

Posted by: admin May 14, 2020 Leave a comment


I have a search worksheet that I use to search a different worksheet. The macro works but I am unable to search two columns for the same word. I tired OR instead of And and that didnt work. I also tried adding another integer but that didnt work either. Here is a little from my macro… basically i want to search column 6 and 7 for the same word.

Sub Searchcustomer()

Dim audit As String
Dim saudit As String
Dim finalrow As Long
Dim i As Long

Set msheet = Sheet11
Set ssheet = Sheet10

audit = ssheet.Range("B8").Value

finalrow = msheet.Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To finalrow
    If  IIf(audit <> "", Cells(i, 6) = audit, True) Then
        msheet.Range(msheet.Cells(i, 1), msheet.Cells(i, 9)).Copy
        msheet.Range(msheet.Cells(i, 1), msheet.Cells(i, 9)).Copy Destination:=ssheet.Range("A100").End(xlUp).Offset(1, 0).Resize(1, 9)

    End If

Next i


End Sub
How to&Answers:
For i = 1 To finalrow
    With msheet.rows(i)
    If  IIf(audit <> "", (.Cells(6) = audit Or .Cells(7) = audit), True) Then

       .Cells(1).Resize(1, 9).Copy _
          Destination:=ssheet.Range("A100").End(xlUp).Offset(1, 0)

    End If
    End With
Next i