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

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:

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:

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.

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 & "!" & _
Else
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

Setting Decimal Tabs in a Table Using the Keyboard

Most people use the mouse to set tab stops in the paragraphs in a table. If you prefer to not use the mouse, then you'll ...

Discover More

Copying a Hyperlink to Lots of Worksheets

Copying information from one place to another in a worksheet is easy. Copying hyperlinks may not seem that easy, but you ...

Discover More

Inserting a Cross-Reference to Text

Cross-referencing is a great feature of Word that allows you to add references to text in various places of your ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Adding Area Codes to Phone Numbers

If you keep phone numbers in an Excel worksheet, you may need a way to add an area code to the beginning of all of the ...

Discover More

Incrementing Numeric Portions of Serial Numbers

If you use serial numbers that include both letters and numbers, you might wonder how you can increment the numeric ...

Discover More

Quickly Adding Formulas Referencing Multiple Worksheets

When you need to pull information from a lot of different worksheets into a single worksheet, it can be baffling to ...

Discover More
Subscribe

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

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 minus 0?

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.

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.

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
sSheet = "'" & rCell.Parent.Name & "'!"
Do
' Range.FindNext does NOT work in a UDF
Set rCell = .Find(What:=Value, After:=rCell)
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

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:
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.

Subscribe

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