0

I am trying to write a code that will allow me to search for multiple phrases like "energy" and "fuel" within multiple cells in a row like C1:F1 and output a 1 to a specific cell like J1 if any of the phrases are found in any cells. These cells may contain multiple words like "directed energy" and I would still like it to output a 1 if it finds the phrase energy anywhere in the cell. I would also like it to be non case-sensitive if possible!! Thank you so much for the help I really truly appreciate it!

Community
  • 1
  • 1

1 Answers1

0

See if this works for you. I want to give @TedWilliams credit for the double "transpose" line, the idea was taken from his excellent post - HERE.

Sub checkString()
    Dim s As String, r As Range, vArr, v
    Dim b As Boolean
    Dim w As Worksheet

    vArr = Array("energy", "fuel") 'items to search for, change to suit

    Set w = ActiveSheet
    Set r = w.Range("C1:F1").Rows(1)

    With Application
        s = Join(.Transpose(.Transpose(r.Value)))
    End With

    For Each v In vArr
        b = (InStr(1, s, CStr(v), vbTextCompare) > 0)
        If b Then Exit For
    Next v

    w.Range("J1").Value = -b
End Sub
Community
  • 1
  • 1
DaveU
  • 1,082
  • 2
  • 14
  • 25