Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Where Is that Text?.

Where Is that Text?

by Allen Wyatt
(last updated January 17, 2015)

7

Jay needs to determine the cell in which a particular text value occurs. He knows he can use Excel's Find and Replace capabilities to manually determine the address of cells containing a text value, but he is looking for a formula to determine the address of the cells. He wonders if there is a way to search for piece of text within a range and have Excel return the address of the cell in which the text is found.

There are two things you can try. First, if you are looking for an exact match for cell contents, then you can use a formula. The basic formula is this:

=ADDRESS(MATCH(C2,A:A,0),1)

In this example, cell C2 contains the value you are looking for and column A is the range of cells being searched. The formula returns a result regardless of the capitalization of C2 or the values in column A. Thus, if C2 contains "apple", then the formula will match positively to cells that contain "apple," "Apple," or "APPLE." Indeed, any mix of capitalization will match.

This formula will not return an address for a cell that contains what you are searching for amidst other text. So if you are searching for "apple" (cell C2), it won't return the address of a cell that contains the phrase "apple crisp." You can modify this behavior, a bit, by adding wild card characters to the search cell. For instance, if you search for "*apple*" then the formula returns the address of a cell that contains "apple", even if it is preceded or followed by other characters.

It should be pointed out that this formula only returns the address of the first cell in the range which meets the criteria. If you actually want the addresses of all cells that meet the criteria, then you'll need to rely on a macro. The following is a good example:

Function FindMe(x As Range, y As String) As String
    Dim r As Range
    Dim sResults As String
    Dim sSearch As String

    Application.Volatile
    sSearch = LCase(y)
    For Each r In x
        If InStr(1, LCase(CStr(r.Value)), sSearch) > 0 Then
            sResults = sResults & r.Address & ", "
        End If
    Next r
    If Len(sResults) > 2 Then
        FindMe = Left(sResults, Len(sResults) - 2)
    Else
        FindMe = ""
    End If
End Function

You use the function by simply providing the range you want to search along with what you want to search for:

=FindMe(A:A, "apple")

If you use a large range (as in this example—all of column A), then don't be surprised if the function takes a noticeable amount of time to return a result. This makes sense, as it has to search through every cell in the range, regardless of whether there is anything in the cell or not.

You also don't need to use any wildcards with this function; it assumes that a match occurs if what you are looking for is located anywhere within the cell. It also doesn't pay attention to the capitalization of what you are looking for or the capitalization of anything in the search range.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10083) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Where Is that Text?.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing a List of AutoCorrect Entries

Want a printed record of the AutoCorrect entries you've created in Word? There is no built-in way to do it, but you can use ...

Discover More

Making Backup Copies

When you save your documents, Word doesn't normally make backups of your files. If you want the program to do that, it takes ...

Discover More

Indenting Cell Contents

Excel allows you to apply several types of alignments to cells. One type of alignment allows you to indent cell contents from ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Relative References to Cells in Other Workbooks

When you construct a formula and click on a cell in a different workbook, an absolute reference to that cell is placed in the ...

Discover More

Evaluating Formulas

Need a bit of help in figuring out how Excel is evaluating a particular formula? It's easy to figure out if you use the ...

Discover More

Retrieving the Last Value in a Column

Need to get at the last value in a column, regardless of how many cells are used within that column? You can apply the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 1?

2016-03-31 13:41:27

Amy

Thank you, Peter! I appreciate the quick response and the help.

Best,
Amy


2016-03-10 18:52:32

Peter Atherton

Amy

You enter the modified function as
=findme(A2:A11,"Fred,Apple") using a comma to separate each value.

Option Explicit

Function FindMe(ref As Range, y As String) As String
'ExcelTips - January 17, 2015
'Modified - 10 Mar 2016
Dim x As Variant
Dim i As Integer
Dim r As Range
Dim sResults As String
Dim sSearch As String
If IsNumeric(InStr(1, y, ",")) Then
x = Split(y, ",")
Else: x = y
End If
Application.Volatile

For Each r In ref
For i = LBound(x) To UBound(x)
sSearch = LCase(x(i))
If InStr(1, LCase(CStr(r.Value)), sSearch) > 0 Then
sResults = sResults & r.Address & ", "
End If
Next i
Next r
If Len(sResults) > 2 Then
FindMe = Left(sResults, Len(sResults) - 2)
Else
FindMe = ""
End If
End Function

Regards


2016-03-09 18:02:34

amy

Hello,
This is great! But I would like to search for multiple strings across that range. For example, not just "apple," but "pie", and "baking".

Is there a way to do this?
I don't need to know which cells contain which string. I only need to get the locations if any of them are found.

Thank you.


2015-05-29 14:04:28

Glenn Case

Paras:

Thanks, appreciate it.


2015-05-28 05:27:12

Paras

Glenn

The logic is as follows :-

In the first case "X as range", The user is prompted to enter the range of data in which the string (y) needs to be searched.
This database can be very large at times and excel will start searching in the data base as soon as it gets any input for Y.

Normally if we want change the cell (or cell value) for "Y" while typing the formula, excel would be busy searching for the value in Range "X" for each change in value, hence the processing would start upfront.


However, In the second case (y As String), the user would finalize the string "Y" to be searched before excel starts searching for it in the range "X".

I guess this is why the second option works faster

Further, I don't think that there would be any affect while defining the variables using DIM (It's purely my guess)



2015-05-27 09:53:19

Glenn Case

Paras:

Why is that?

Is this also true for the order of variables dimensioned using DIM statements? Where might I find a list of the the preferred hierarchy for dimensioning variable types?


2015-05-26 09:01:26

Paras

The code will become much faster, if we change the first line of code

Function FindMe(x As Range, y As String) As String

to

Function FindMe(y As String, x As Range, ) As String


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.