Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. 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: Finding Text in Text Boxes.
by Allen Wyatt
(last updated September 7, 2015)
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.TextFrame2.TextRange.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.TextFrame2.TextRange.Characters.Text) iPos = InStr(sTemp, sFind) If iPos > 0 Then With shp.TextFrame2.TextRange.Characters(Start:=iPos, _ Length:=Len(sFind)).Font .UnderlineStyle = msoUnderlineHeavyLine .Bold = True End With End If Next MsgBox "Finished" End Sub
This macro underlines the located text using a heavy line, and then makes it bold. 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.TextFrame2.TextRange.Characters.Font .UnderlineStyle = msoNoUnderline .Bold = False End With Next End Sub
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11282) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Finding Text in Text Boxes.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Text boxes are handy for placing information in a container that can "float" over your worksheet. This tip explains what ...Discover More
Adding a text box to a worksheet is easy. Making sure that text box is the exact size of a cell in the worksheet may not ...Discover More
Want to get rid of your text boxes and move their text into the worksheet? It's going to take a macro-based approach, ...Discover More
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.