Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Searching for All.

Searching for All

Written by Allen Wyatt (last updated December 31, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


Jack wonders how he can do a search for a certain word or phrase and, in one step, highlight all the cells containing it so that he can cut or copy them and paste them elsewhere.

Selecting the cells containing the text you want to use is rather easy; you can use the standard Find and Replace feature to do it. Follow these steps:

  1. Press Ctrl+F. Excel displays the Find tab of the Find and Replace dialog box.
  2. Expand the dialog box by clicking the Options button. (See Figure 1.)
  3. Figure 1. The Find tab of the Find and Replace dialog box.

  4. In the Find What box, enter the text you want to find.
  5. Use the controls in the dialog box to limit the matches, as desired.
  6. Click Find All. The dialog box is expanded to show all the matches that were located.
  7. Press Ctrl+A. This selects all those cells that were found.
  8. Click Close to dismiss the dialog box.

That's it. As long as you didn't click on Match Entire Cell Contents in step 4, Excel selects all the cells that contain the text you specified in step 3. You can, at that point, apply formatting to the cells, if desired.

You could, of course, use conditional formatting to dynamically format cells that contain the text you want to highlight. All you need to do is set up a condition that uses a formula that tests for the presence of the text you want. This won't, of course, select all the cells that contain the text, but it will highlight them so you can pick out where they are.

You could also use a macro to select all the cells that contain the desired text. The following is a rather simple one that accomplishes the task:

Sub selCellbasedonValue()
    Dim c As Object
    Dim u As Range
    Dim v As Range
    Dim sInpt As String

    Set u = ActiveSheet.UsedRange

    sInpt = InputBox("Enter the search text")

    If sInpt > "" Then
        For Each c In u
            If Instr(LCase(sInpt),LCase(c.Value)) > 0 Then
                If v Is Nothing Then
                    Set v = Range(c.Address)
                Else
                    Set v = Union(v, Range(c.Address))
                End If
            End If
        Next
        v.Select
        Set v = Nothing
    End If

    Set u = Nothing
End Sub

There is a problem with selecting cells that you need to recognize, however—if the cells are non-contiguous, you cannot cut or copy the cells. If you try, you'll get an error message indicating that the command cannot be used on multiple selections. The easiest way to copy cell contents to a different location is to, again, use a macro:

Sub CopyFinds()
    Dim sSrch As String
    Dim sFirst As String
    Dim rPaste As Range
    Dim i As Integer
    Dim iLeftC As Integer
    Dim lTopR As Long
    Dim c As Object

    If Selection.Cells.Count = 1 Then
        MsgBox "Select the range to be searched."
        Exit Sub
    End If

    'Specify search string
    sSrch = InputBox(Prompt:="Enter the search text")

'   Set the paste address
    On Error Resume Next
    Set rPaste = Application.InputBox(Prompt:="Enter the upper-left " & _
      "cell address for the paste range", Type:=8)
    On Error GoTo 0

'   Exit if canceled
    If TypeName(rPaste) <> "Range" Then Exit Sub

'   Upper left cell to be used
    Set rPaste = rPaste.Range("A1")

    'Set where paste will start and headings
    Application.ScreenUpdating = False
    lTopR = rPaste.Row
    iLeftC = rPaste.Column
    Cells(lTopR, iLeftC) = "Address"
    Cells(lTopR, iLeftC + 1) = "Cell Value"
    lTopR = lTopR + 1
    
    'Start copying cell values
    With Selection
        Set c = .Find(What:=sSrch, LookAt:=xlPart, MatchCase:=True)
        If Not c Is Nothing Then
            sFirst = c.Address
            Do
                Cells(lTopR, iLeftC) = c.Address
                Cells(lTopR, iLeftC + 1) = c.Value
                Set c = .FindNext(c)
                lTopR = lTopR + 1
            Loop While Not c Is Nothing And c.Address <> sFirst
        End If
    End With
    
    Application.ScreenUpdating = True
    Cells(rPaste.Row, rPaste.Column).Select
End Sub

When you select a range of cells and run this macro, you are asked to specify what you are searching for (case is important) and an address of where you want to copy it. The macro then finds all cells that contain that value and copy both their address and the cell value to the starting address you specified. The macro doesn't do a lot of error checking; it will overwrite information if you specify a target address that has information in it already. In addition, if you specify a target address that is within the range you are searching, the macro may run infinitely. You should definitely specify a target that is outside of the range being searched.

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 (8963) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Searching for All.

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 Print Quality

When printing information in a workbook, you may want to take advantage of the different print quality settings available ...

Discover More

Sending Single Worksheets via E-mail

Got a single worksheet that you want to e-mail to someone, but don't want them to see the rest of the worksheets in the ...

Discover More

Copying Comments when Filtering

The filtering feature in Excel allows you to quickly copy unique information from one data list to another. If you want ...

Discover More

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!

More ExcelTips (menu)

Searching for Wildcards

Wildcard characters can be used within the Find and Replace tool, but what if you want to actually search for those ...

Discover More

Making All Occurrences Bold

Want to make instances of a given word or phrase bold throughout a worksheet? Here's a way you can make the change quickly.

Discover More

Searching by Columns, by Default

Do you often want to search through a worksheet by column rather than by row? Excel defaults to searching by row, of ...

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 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.