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: Finding Text in Text Boxes.

Finding Text in Text Boxes

Written by Allen Wyatt (last updated October 12, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


11

Walter has a worksheet that has a number of text boxes in it. He would like to search through those text boxes to find some specific text, but Find and Replace doesn't seem capable of finding text in text boxes. He wonders if there is a way to search through text boxes.

Walter is right; you cannot find text located in text boxes in Excel. To test this, we opened a brand new workbook, placed a single phrase in it ("my message"), and then placed some random text and numbers in other cells in the worksheet. Then, with the text box not selected, Ctrl+F was pressed to search for "my message." Excel dutifully reported that it couldn't find the text, even though it was still right there, in the text box.

Fortunately, you can search for text in a text box using a macro. Each text box in a worksheet belongs to the Shapes collection, so all you need to do is step through each member of the collection and see if it contains the desired text. Here's a macro that prompts for a search string and then looks for it in the text boxes.

Sub FindInShape1()
    Dim rStart As Range
    Dim shp As Shape
    Dim sFind As String
    Dim sTemp As String
    Dim Response

    sFind = InputBox("Search for?")
    If Trim(sFind) = "" Then
        MsgBox "Nothing entered"
        Exit Sub
    End If
    Set rStart = ActiveCell
    For Each shp In ActiveSheet.Shapes
        sTemp = shp.TextFrame.Characters.Text
        If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then
            shp.Select
            Response = MsgBox( _
              prompt:=shp.Name & vbCrLf & _
              sTemp & vbCrLf & vbCrLf & _
              "Do you want to continue?", _
              Buttons:=vbYesNo, Title:="Continue?")
            If Response <> vbYes Then
                Set rStart = Nothing
                Exit Sub
            End If
        End If
    Next
    MsgBox "No more found"
    rStart.Select
    Set rStart = Nothing
End Sub

This macro looks through all the shapes in the worksheet, not just the text boxes. If you prefer to limit your search to only text boxes, you can step through the TextBoxes collection instead of the Shapes collection; either way will work fine.

Notice, as well, that this approach stops each time it finds matching text (the case of the text doesn't matter) and asks you if you want to continue. You may, instead, want a macro that simply marks the matching text in text boxes. This can be done with a shorter macro, as shown here:

Sub FindInShape2()
    Dim shp As Shape
    Dim sFind As String
    Dim sTemp As String
    Dim iPos As Integer
    Dim Response

    sFind = InputBox("Search for?")
    If Trim(sFind) = "" Then
        MsgBox "Nothing entered"
        Exit Sub
    End If
    sFind = LCase(sFind)
    For Each shp In ActiveSheet.Shapes
        sTemp = LCase(shp.TextFrame.Characters.Text)
        iPos = InStr(sTemp, sFind)
        If iPos > 0 Then
            With shp.TextFrame.Characters(Start:=iPos, _
              Length:=Len(sFind)).Font
                .ColorIndex = 3
                .Bold = True
            End With
        End If
    Next
    MsgBox "Finished"
End Sub

This macro highlights the located text using a bold, red font. When you are done, you probably want to change the text back to regular text. You can do so by using the following macro:

Sub ResetFont()
    Dim shp As Shape

    For Each shp In ActiveSheet.Shapes
        With shp.TextFrame.Characters.Font
            .ColorIndex = 0
            .Bold = False
        End With
    Next
End Sub

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 (11281) 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: Finding Text in Text Boxes.

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

Using the Style Area

The style area is an esoteric feature of Word that allows you to easily see the styles applied to the paragraphs in your ...

Discover More

Setting Table Values to Three Decimal Places

If you import information into a document from another program, the values you import may not be exactly to your liking. ...

Discover More

Setting Up Your Printer

Word allows you to take full advantage of the capabilities of your printer. Accessing those capabilities is done through ...

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)

Checking for Messages in Cells

If you have a range of cells used to display error messages, you soon discover that it is easy to miss messages that may ...

Discover More

Wildcards in 'Replace With' Text

When doing searches in Excel, you can use wildcard characters in the specification of what you are searching. However, ...

Discover More

Changing Default Search Settings

Excel provides some great tools for finding information in a worksheet or a workbook. Changing the default settings used ...

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?

2023-03-04 18:39:43

Arlene Zimmerly

In the first macro, I also get an error 1004 on sTemp = shp.TextFrame.Characters.Text
Why?


2022-10-20 01:03:52

Lynton

Hi Allen

I created the macro and found that it doesn't work - i got a runtime error "The specified value is out of range"

Would appreciate any tips!

Thanks

Lynton


2022-06-10 01:56:07

Joao Casqueiro

Hi,
In the first macro, it's giving me error 1004 on sTemp = shp.TextFrame.Characters.Text
Any hints?
Thanks/Brgds
joao


2021-07-21 10:40:40

Keith Smith

I created a map of our network using shapes with text inside. Today realized I need to search the contents and cannot. I pasted the code above, saved as a Excel Macro file. "No more found" as if it is not seeing the text inside the shapes. Does this only work for Text Boxes and not the Text in the Shapes?


2020-12-14 13:45:43

mark

Hi guys, I found a way to export all the Excel data and make it searchable in Word.
You can see the method here: https://stackoverflow.com/a/63436101


2018-10-23 23:47:28

Chris

Getting "Run-time error 438 object doesn't support this property or method" on this line in FindInShape2: sTemp = LCase(shp.TextFrame.Characters.Text).

From my reading this is because not every shape object will necessarily have the TextFrame, Characters, Text, etc. This is resolved by adding the following line as the first thing inseach each "for each" loop:

On Error Resume Next

As a result, any shape not being able to reference right down through to the "Text" object, will be skipped.


2018-06-20 09:45:14

Pascale Bourque

Your search in textbox macros are amazing!

I would like to do the followings:
1- Search in whole workbook instead of active sheet.
2-Change the color of the text box in addition to flagging text in red font (of course, the macro for return to normal should always bring the color of the text box back to normal).

Thanks for your help!

Pascale


2018-04-03 20:18:53

Rod Wenban

tested for Textboxes only current Office 365 April 2018..

Sub FindInShape1()
Dim rStart As Range
Dim shp As TextBox '****different****
Dim sFind As String
Dim sTemp As String
Dim Response

sFind = InputBox("Search for?")
If Trim(sFind) = "" Then
MsgBox "Nothing entered"
Exit Sub
End If
Set rStart = ActiveCell
For Each shp In ActiveSheet.TextBoxes '****different****
sTemp = shp.Text

If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then
shp.Select
Response = MsgBox( _
prompt:=shp.Name & vbCrLf & _
sTemp & vbCrLf & vbCrLf & _
"Do you want to continue?", _
Buttons:=vbYesNo, Title:="Continue?")
If Response <> vbYes Then
Set rStart = Nothing
Exit Sub
End If
End If
Next
MsgBox "No more found"
rStart.Select
Set rStart = Nothing
End Sub


2017-12-04 07:43:16

seb

Hi,

I get an error in Sub FindInShape2() at this line :

sTemp = LCase(shp.TextFrame.Characters.Text)


2017-09-22 08:05:46

Kasper

Hey Allan,

I read your article and I want to implement this into an excel-file that we constantly use at work.
I tried out your code in a small test excel-file and it works great. But as soon as I try it in the actual escel-file I want to use it in I get an error code 438 "Object doesn't support this property or method."
What do you think is the problem? I included a picture of the file I want to run your code in.
I hope you can help me, this working would automate an important aspect of our working.
Thanks already for the first step with this wonderfull code.
(see Figure 1 below)


Figure 1. 




2017-04-13 16:18:04

john

This is great information, but I have a question. What happens if a word appears in a text box more than once? When I look at your code it appears to only change the font if it appears once, but what if you want "call out" the word more than once?


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.