Home » excel » How to check if a substring is in any of the entries in rows of an excel sheet?

How to check if a substring is in any of the entries in rows of an excel sheet?

Posted by: admin May 14, 2020 Leave a comment

Questions:

So I basically have a table with multiple columns.

enter image description here

So for each of the ids (1, 2, 3), I would like to check in which column there is sub-string of * (as you see sometimes it’s in B and sometimes in C). Then I would like to extract the whole string that contains * and is associated with the given ID.

Suppose that my actual table contains over 10 columns – but the idea remains the same.
In other words the entries that I am looking for that contain a specific substring are scatted all throughout the 10 different columns.

How to&Answers:

Use HLOOKUP

=HLOOKUP("*~**",B1:C1,1,FALSE)

Since the asterisk(*) is a wildcard we need to append it with the tilde(~) to tell Search to look for the actual character.

The outer * allow the HLOOKUP to look at part.

enter image description here

Answer:

Try the following User Defined Function:

Public Function FindTheStar(rng As Range) As String
    Dim r As Range, v As String
    FindTheStar = ""
    For Each r In rng
        v = r.Text
        If InStr(v, "*") > 0 Then
            FindTheStar = v
            Exit Function
        End If
    Next r
End Function

It will find and return the first cell in any range that contains an asterisk.

enter image description here