Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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: Searching for a Value Using a Function.

Searching for a Value Using a Function

Written by Allen Wyatt (last updated July 9, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


4

Thor wonders if there is a way to perform a lookup without having to specify a column or row and having the result be the address of the cell at which the value is found. For instance, he wants to look up a value (such as 345 or "my text") and have the function search all the cells in all the worksheets in the workbook and return the full address of the cell in which the value was found.

The approach you use will be dictated by the range you want to search. If you want to search on the same worksheet on which you want the answer displayed, then you can use a formula, such as the following:

=ADDRESS(MAX(ROW(1:5)*(A1:E5="my text")),
MAX(COLUMN(A1:E1)*(A1:E5="my text")),4)

This should be entered as an array formula (press Ctrl+Shift+Enter) if you are not using Excel 2019, Excel 2021, or the version of Excel provided with Microsoft 365. (If you are using one of those versions, then just enter the formula normally.) It only searches in the range A1:E5. You can, if desired, change the range by adjusting the formula appropriately.

A larger search area would be to look at an entire worksheet. This can still be done using a formula, such as the following:

=ADDRESS(MAX(ROW(Sheet1!1:1048576)*(IF(Sheet1!1:1048576=$A$1,1,0))),
MAX(COLUMN(Sheet1!$1:$1048576)*IF(Sheet1!1:1048576=$A$1,1,0)))

The formula assumes that what you are looking for is stored in cell A1. You should change the Sheet1 designation to the name of whatever worksheet you want searched. Again, if you are using Excel 2019, Excel 2021, or the version of Excel with Microsoft 365, you can enter this as a regular formula. If you are using an older version of Excel, you should enter it as an array formula by pressing Ctrl+Shift+Enter.

If you want to search a wider range, such as all the worksheets in a workbook, then the best solution is to use a macro that calls upon the Find function within Excel.

Function FindAddr(vValue As Variant)
    Dim wks As Worksheet
    Dim rCell As Range
    Dim bFound As Boolean

    bFound = False
    For Each wks In ActiveWorkbook.Worksheets
        With wks
            Set rCell = .Cells.Find _
              (What:=vValue, After:=.Cells(1), _
              LookIn:=xlValues, LookAt:=xlWhole, _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlNext, _
              MatchCase:=False)
            If Not rCell Is Nothing Then
                bFound = True
                Exit For
            End If
        End With
    Next
    If bFound Then
        FindAddr = wks.Name & "!" & _
          rCell.Address(False, False)
    Else
        FindAddr = "Not Found"
    End If
    Set wks = Nothing
    Set rCell = Nothing
End Function

This function is designed to be called from another macro, which passes it whatever should be searched for in the vValue parameter. The function returns either the full address (including worksheet name) of the first match, or it returns "Not Found" if there was no match.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11524) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Searching for a Value Using a Function.

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

Searching for Breaks

Word allows you to insert different types of breaks in your text that help control how your document is paginated. If you ...

Discover More

Shading Rows with Conditional Formatting

If you need to shade alternating rows in a data table, you'll want to examine how you can accomplish the task with ...

Discover More

Limiting Who Can Delete Data

Excel allows you to protect your worksheet data in several different ways. Unfortunately, it doesn't allow you to protect ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Averaging Based on Multiple Criteria

When you need to determine an average based on a very small selection of cells from a large dataset, based on multiple ...

Discover More

Calculating a Geometric Standard Deviation

One of the areas in which Excel provides worksheet functions is in the arena of statistical analysis. You may want to ...

Discover More

Counting Wins and Losses

Need to count the number of W (win) or L (loss) characters in a range of cells? You can develop a number of formulaic ...

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}] (all 7 characters, in the sequence shown) 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 two more than 7?

2022-10-01 12:46:58

J. Woolley

Sorry, but I rearranged parameters for the ListMatchingCells function described in my previous comments below. Here is the new syntax:
=ListMatchingCells(Value,[LookIn],[MatchCase],[MatchEntire],[AllSheets])
Value can be a numeric or text constant or a formula's result or a cell (like A1) with a non-blank value; * and ? are wild card characters and ~ is the escape character, the same as Find (Ctrl+F).
The first character of LookIn (case ignored) must be V for Values (default), F for Formulas, N for Notes (legacy unthreaded comments), C for Comments (threaded), or A for All (V, F, N, and C).
If MatchCase is FALSE (default), alphabetic case will be ignored when Value is text; if TRUE, the case of a text Value must match.
If MatchEntire is FALSE (default), a partial match is accepted; if TRUE, the entire content must match.
If AllSheets is FALSE (default), only the current worksheet will be evaluated; if TRUE, all worksheets in the workbook will be included.
See https://sites.google.com/view/MyExcelToolbox/


2022-09-30 12:08:35

J. Woolley

Two optional parameters have been added to the ListMatchingCells function described in my previous comment below. Here is the new syntax:
=ListMatchingCells(Value,[MatchCase],[AllSheets],[LookIn],[LookAt])
The first character of LookIn must be V for Values (default), F for Formulas, N for Notes (legacy unthreaded comments), C for Comments (threaded), or A for All (V, F, N, and C).
The first character of LookAt must be P for Partial (default) or W for Whole (match entire cell).
Alphabetic case of LookIn and LookAt is ignored.
See https://sites.google.com/view/MyExcelToolbox/


2022-08-05 14:46:37

J. Woolley

My Excel Toolbox includes the following dynamic array function to return a list of cells matching Value:
=ListMatchingCells(Value,[MatchCase],[AllSheets])
Value can be a numeric or text constant or a formula's result or a cell (like A1) with a non-blank value.
If MatchCase is FALSE (default), alphabetic case will be ignored when Value is text; if TRUE, the case of a text Value must match.
If AllSheets is FALSE (default), only the current worksheet will be evaluated; if TRUE, all worksheets in the workbook will be included.
Excel 365 or 2021+ is required when this function is used in a cell formula; it must NOT be used with My Excel Toolbox's SpillArray function (directly or indirectly). In older versions of Excel, it can be called by a macro (Sub) with results in a MsgBox or posted to a worksheet.
Here is a simplified version:

Function ListMatchingCells(Value, Optional MatchCase)
    Dim C As Collection, rCell As Range, n As Long
    Dim sAddr As String, sFirst As String, sSheet As String
    Application.Volatile
    If Value = vbNullString Then
        ListMatchingCells = CVErr(xlErrValue)
        Exit Function
    End If
    Set C = New Collection
    C.Add "Cells matching '" & Value & "'"
    With Application.Caller.Worksheet.UsedRange
        Set rCell = .Cells((.Rows.Count - .Row + 1), _
            (.Columns.Count - .Column + 1)) ' last cell
        Set rCell = .Find(What:=Value, After:=rCell, _
            LookIn:=xlValues, LookAt:=xlWhole, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext, _
            MatchCase:=MatchCase)
        If Not (rCell Is Nothing) Then
            sAddr = rCell.Address
            sFirst = sAddr
            sSheet = "'" & rCell.Parent.Name & "'!"
            Do
                C.Add sSheet & sAddr
                ' Range.FindNext does NOT work in a UDF
                Set rCell = .Find(What:=Value, After:=rCell)
                sAddr = rCell.Address
            Loop Until sAddr = sFirst
        End If
    End With
    If C.Count = 1 Then C.Add "None"
    ReDim A(1 To C.Count, 1 To 1) As Variant
    For n = 1 To C.Count
        A(n, 1) = C(n)
    Next n
    Set C = Nothing
    ListMatchingCells = A
End Function

See https://sites.google.com/view/MyExcelToolbox/


2022-07-09 10:42:25

J. Woolley

The Tip's FindAddr user-defined function (UDF) can also be used in a cell formula. For example:
=FindAddr(345)
=FindAddr("my text")
=FindAddr($A$1)
=$A$1&" @ "&FindAddr($A$1)
It searches cells in each worksheet 1 to Worksheets.Count, columns left-to-right, rows top-to-bottom, and returns only the first cell with a value that wholly matches the searched value ignoring alphabetic case and cell format. For each worksheet, cell $A$1 is searched last.
The UDF could be modified to return all matching cells as an array.


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.