In Excel VBA, use the Range.Find method to search a specified range; check whether it returned a cell before using the result. For a reliable search, explicitly set options such as LookIn, LookAt, and MatchCase, because Excel remembers some Find settings between searches.
What Range.Find does
Range.Find searches cells and returns a Range object for the first matching cell. It does not return the matching text itself: use properties such as foundCell.Value, foundCell.Address, or foundCell.Row to inspect the result. If there is no match, the method returns Nothing, so VBA code must test for that before accessing the result.
The range you call Find on defines the search area. For example, Worksheets("Orders").Range("A2:A500") searches only those cells, while Worksheets("Orders").UsedRange searches the worksheet’s used range. A narrowly defined range is usually faster and prevents a match elsewhere on the sheet from being mistaken for the intended result.
The search does not change the cells. It locates a match so your macro can report it, read it, or take another action. Searching is separate from changing data; if later code edits or deletes cells, make a copy of the workbook first because macros cannot be undone with Ctrl+Z.
A basic VBA example
This example looks for an exact, case-insensitive match in column A of the Orders sheet. It searches cell values rather than formula text and reports the address of the first match.
Before running any macro that may affect workbook data, save a copy of the workbook. To add this code in Excel for Microsoft 365 on Windows, press Alt+F11, choose Insert > Module in the Visual Basic Editor, and paste the procedure into the module. On Mac, Option+F11 opens the editor on many keyboards; some require Fn as well. The VBA code is the same, although Mac keyboard settings can affect the shortcut.
Sub FindExactCustomer()
Dim ws As Worksheet
Dim searchRange As Range
Dim foundCell As Range
' Limit the search to the customer-name column.
Set ws = ThisWorkbook.Worksheets("Orders")
Set searchRange = ws.Range("A2:A500")
' Set every important option instead of relying on saved Find settings.
Set foundCell = searchRange.Find( _
What:="Northwind", _
After:=searchRange.Cells(searchRange.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If foundCell Is Nothing Then
MsgBox "Customer not found."
Else
MsgBox "Found at " & foundCell.Address(False, False)
End If
End Sub
After must refer to a single cell inside the searched range. Excel begins after that cell and wraps around, so using the last cell as the starting point lets the search consider the full range. The method returns a cell reference; Address(False, False) displays a relative-style address such as A17.
Choosing the search options
The optional arguments determine what counts as a match and how Excel searches. Supplying them explicitly makes a macro more predictable than relying on whichever settings a user last chose in Excel’s Find dialog.
Whatis the text, number, or other value to locate.LookIn:=xlValuessearches cell values;LookIn:=xlFormulassearches formula content. For example, a formula cell can be searched by its calculated result withxlValuesor by formula text withxlFormulas.LookAt:=xlWholerequires the entire cell content to match.LookAt:=xlPartaccepts a match within the cell, so searching forNorthcan findNorthwind.SearchOrder:=xlByRowschecks across rows;xlByColumnschecks down columns. This matters when deciding which of several matches is returned first.SearchDirection:=xlNextsearches forward through the range;xlPrevioussearches in the reverse direction.MatchCase:=Truedistinguishes uppercase and lowercase;Falsedoes not.SearchFormat:=Falseprevents an existing format criterion from affecting the search. If searching by formatting is intentional, configure the Find format criteria deliberately.
Excel also supports wildcard searches. An asterisk (*) can represent any number of characters, and a question mark (?) can represent one character. Put a tilde before a wildcard character to search for it literally: for example, ~* searches for an actual asterisk. Wildcards are useful for variable text, but use xlWhole when the complete cell must satisfy the pattern.
Finding every matching cell
Find returns only the first match in its search order. To process all matches, store the first match’s address, then repeatedly call FindNext until the search returns to that address. The address check is essential because Excel wraps around to the beginning of the range instead of stopping at the last cell.
This example prints the addresses and values of every cell in A2:A500 whose value exactly matches Northwind, without changing the worksheet.
Save a copy of the workbook before running a macro, particularly if you adapt the example to modify cells. Macros cannot be reversed with Ctrl+Z.
Sub FindAllCustomers()
Dim ws As Worksheet
Dim searchRange As Range
Dim foundCell As Range
Dim firstAddress As String
' Define the worksheet and cells to search.
Set ws = ThisWorkbook.Worksheets("Orders")
Set searchRange = ws.Range("A2:A500")
' Find the first exact match.
Set foundCell = searchRange.Find( _
What:="Northwind", _
After:=searchRange.Cells(searchRange.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not foundCell Is Nothing Then
firstAddress = foundCell.Address
Do
Debug.Print foundCell.Address(False, False), foundCell.Value
Set foundCell = searchRange.FindNext(After:=foundCell)
If foundCell Is Nothing Then Exit Do
Loop While foundCell.Address <> firstAddress
End If
End Sub
Open the Immediate window in the Visual Basic Editor with Ctrl+G on Windows to view the output from Debug.Print. This example does not show a message when no results are found; the If Not foundCell Is Nothing condition simply skips the loop. Keep the range and all Find criteria consistent between the initial search and subsequent calls.
Searching formulas, values, and displayed text
A frequent source of confusion is the difference between a formula and its result. If cell B2 contains =A2&" Ltd", LookIn:=xlFormulas searches the formula expression, while LookIn:=xlValues searches the value calculated for the cell. Choose according to the question your macro is answering: “Does this formula contain a reference?” differs from “Does this cell’s result contain this name?”
Displayed appearance is not always the same as underlying content. A date may be stored as a number and displayed using a date format; a number may show rounded digits while retaining more precision in the cell. If a search depends on formatted display text, test it against representative cells and consider reading the cell’s Text property instead. That property reflects the displayed text, which can also be affected by column width, so it is not a universal substitute for searching values.
For formula cells that return an empty string, decide whether the macro should treat the formula as present or the displayed result as blank. Searching formulas and searching calculated values can answer different questions. If the requirement is specifically to identify blank cells, a dedicated check such as IsEmpty or a comparison with an empty string may be clearer than a general text search.
Common problems and how to avoid them
- The macro reports no match unexpectedly: Check the worksheet name, range boundaries, spelling, and whether
xlWholeis too strict. If the search value is part of a longer cell entry, usexlPart. - The wrong match appears first: Review
SearchOrder,SearchDirection, and theAftercell. A range can contain multiple matches, and changing row-versus-column order can change which match appears first. - Results change after using Find on the worksheet: Excel retains several Find options between calls and shares search settings with the Find dialog. Specify all relevant arguments in VBA, especially
LookIn,LookAt, andSearchFormat. - VBA stops with an object error: The result may be
Nothing. Always check it before readingValue,Address, or another property. - The intended sheet is not searched: Qualify the range with a worksheet reference. Unqualified
Rangecan refer to the active sheet, which may not be the sheet the macro should use. - The workbook is not the one expected:
ThisWorkbookmeans the workbook containing the VBA code.ActiveWorkbookmeans the workbook currently active in Excel. If code is stored in an add-in or Personal Macro Workbook, these can be different files.
For workbooks shared with others, also confirm that macros are allowed under the organization’s security policy. A macro-enabled workbook normally needs the .xlsm file format to retain VBA code. Excel for the web can open macro-enabled files but cannot create, run, or edit VBA macros; open the workbook in the desktop Excel application for this code.
Range.Find compared with other VBA approaches
Use Range.Find when the goal is to locate a matching cell in a range, particularly when Excel’s built-in matching choices—partial or whole-cell, formula or value, direction, and case—are useful. It is compact and can search a large range without writing a cell-by-cell comparison loop.
A For Each loop can be a better choice when every cell needs custom logic that Find does not express, such as checking multiple properties or applying a complicated rule. For example, a loop can inspect both a cell’s value and its number format. The trade-off is that code must handle the range traversal and comparison itself, and a poorly scoped loop may be slower on large worksheets.
For exact lookup tasks that need a corresponding value rather than a cell address, a worksheet lookup function may be more appropriate. VBA’s Find is not a replacement for every lookup: it is particularly useful when the macro needs the matching cell itself, its location, or repeated matches. Choose the method based on whether the task is locating, returning a related value, or evaluating custom conditions.
Frequently Asked Questions
How do I find a value in a specific Excel column with VBA?
Set a range for that column, such as ws.Range("A2:A500"), and call its Find method. Specify the search options and check whether the returned object is Nothing.
What is the difference between xlWhole and xlPart?xlWhole matches the entire cell content, while xlPart matches text within the content. Searching for “Ann” with xlPart may match “Ann Lee”; with xlWhole, it will not.
How can I make VBA Find ignore uppercase and lowercase?
Set MatchCase:=False. Set it to True only when capitalization must distinguish otherwise identical text.
Why does Range.Find return Nothing?
It means no cell matched within the range under the specified search options. Check the range, the value being searched, whether the search should use xlValues or xlFormulas, and whether whole-cell matching is required.
Can Excel for the web run a VBA Find macro?
No. Excel for the web does not run VBA macros. Use desktop Excel for Windows or Mac to run the macro; the VBA Find method is available in the desktop application.
Related
- Excel Page Break Preview Without Page Numbers: What to Know
- Excel Page Break Preview: Add and Move Page Breaks
- Excel Page Break Preview: How to View and Adjust Pages
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Find Next: Search Matches and Continue SafelyIn Excel VBA, use Range.Find to locate the first match and Range.FindNext to continue to the next one.…
- VBA MsgBox New Line: Add Line Breaks to MessagesTo add a new line in a VBA MsgBox, join the message text with vbCrLf or vbNewLine where…
- MS Access VBA MsgBox Examples: Syntax, Buttons and UseIn Microsoft Access VBA, use MsgBox to show a message or ask a user a question; combine button…
- Excel VBA Select Range Using Cells: Examples and TipsIn Excel VBA, use Cells(rowNumber, columnNumber) to identify a cell, then call Select on that cell or on…
- Excel VBA Pivot Table Filter Criteria: ExamplesIn Excel VBA, apply PivotTable filter criteria through the relevant PivotField: use CurrentPage for a single report-filter selection,…
- VBA Range Variables: How to Declare and Use ThemA VBA Range variable is an object variable that refers to one or more cells in an Excel…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.