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.
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:
=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:
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Given a range of cells, you may at some time want to calculate the sum of only the largest values in that range. Here is ...
Discover MoreIf you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...
Discover MoreSometimes making sure that a reference in a formula doesn't get changed is not as simple as putting dollar signs in front ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments