Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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 and Replacing in Text Boxes.

Finding and Replacing in Text Boxes

Written by Allen Wyatt (last updated June 6, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


9

David wonders if it is possible to use Find and Replace to locate and modify text in text boxes or in labels in charts. The short answer is that it is not possible, but there are several workarounds you can try.

First, you could easily make the text in your text boxes or in your chart labels dynamic, so that it is tied to the contents of some worksheet cells. For instance, you could do the following for your text boxes:

  1. Copy your text from each of text boxes to a range of cells on your worksheet. (For this example, assume that you copied the contents of ten text boxes to the range Z1:Z10.)
  2. Select the first text box (the one that corresponds to cell Z1) and get rid of the text box's contents.
  3. With the text box still selected, enter the following into the Formula bar: =Z1. When you press Enter, the text box should reflect whatever is in cell Z1.
  4. Repeat steps 2 and 3 for each of your other text boxes, using the appropriate cell reference for each in step 3.

That's it. You can use the same technique with custom chart labels—all you need to do is select the chart label and enter a cell reference in the Formula bar. With the text boxes and chart labels tied to worksheet cells, you can easily use Find and Replace to search for and change information in the cells. When the changes are made, the text boxes and chart labels should automatically reflect the changes in the cells.

The only way to actually change the text within a text box or chart label is to change it manually or change it using a macro. The code would need to step through each text box in the worksheet and then make your change. The following is a simple version of a macro that can make such a change.

Sub TextBoxReplace()
    Dim shp As Shape
    Dim sOld As String
    Dim sNew As String

    'Change as desired
    sOld = "Old string"
    sNew = "New string"
    On Error Resume Next
    For Each shp In ActiveSheet.Shapes
        With shp.TextFrame.Characters
            .Text = Application.WorksheetFunction.Substitute( _
              .Text, sOld, sNew)
        End With
    Next
End Sub

This macro steps through all the shapes in the worksheet (text boxes are shapes) and then replaces whatever is in the sOld variable with whatever is in the sNew variable. Applying the same technique to chart labels is only a bit more complex, as shown in the following macro:

Sub ChartLabelReplace()
    Dim Cht As ChartObject
    Dim Ser As Series
    Dim scPt As Point
    Dim sOld As String
    Dim sNew As String

    'Change as desired
    sOld = "Old String"
    sNew = "New String"
    On Error Resume Next
    For Each Cht In ActiveSheet.ChartObjects
        For Each Ser In Cht.Chart.SeriesCollection
            For Each scPt In Ser.Points
                With scPt.DataLabel
                    .Text = Application.WorksheetFunction.Substitute( _
                      .Text, sOld, sNew)
                End With
            Next
        Next
    Next
End Sub

The macro steps through each data label for every data series on every chart and (again) replaces any instances of whatever is in sOld with whatever is in sNew.

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 (9264) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Finding and Replacing 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

Canceling a Command

Need to cancel a command you've already started? It is as easy as pressing a single keystroke.

Discover More

Delivery Address Won't Print on Envelopes

Word includes a feature that allows you to easily create and print envelopes, based on the addresses you insert in your ...

Discover More

Picking Different Random Numbers from a Range

It is not unusual to need to select two random items from a list. There are a couple of ways you can approach the task; ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Finding All Instances of a Value

Searching for information in an Excel worksheet generally goes very smoothly. There can be times, however, when the ...

Discover More

Searching for Leading Apostrophes

Take a look at the Formula bar when you select a cell that contains text, and you may see an apostrophe at the beginning ...

Discover More

Replacing in Worksheets and Comments At the Same Time

If you need to replace information that may appear in cells, comments, and text boxes, your best bet is to use a macro. ...

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?

2022-07-26 11:48:47

J. Woolley

@J Stern
My Excel Toolbox's dynamic array function ListShapes is described here: https://excelribbon.tips.net/T009726_Positioning_a_Graphic_in_a_Macro.html
The list includes each shape's link formula. The ReplaceShapeText macro described in my comment below also identifes a shape's link formula. Neither attempts to modify the formula.


2022-07-21 16:19:22

J. Woolley

The ReplaceShapeText macro in My Excel Toolbox will find and offer to replace specified text in each shape on the active sheet or on all sheets of the active workbook. It includes Steve's logic to search grouped shapes plus options to match alphabetic case, whole words, and hidden shapes (such as hidden Comments). Text formatting is preserved.
This macro does not apply to chart labels but does apply to text boxes or other shapes added to charts.
Developing this macro presented several interesting user-interface challenges. Perhaps it should be revisited sometime with a UserForm.
See https://sites.google.com/view/MyExcelToolbox/


2022-07-03 15:40:29

J. Woolley

I should have included in my previous comment: The revised TextBoxReplace macro also affects Unthreaded Comments (Notes) because they are part of the Shapes collection. But curiously, in this case sNew will inherit the format of the character BEFORE the first character of sOld.


2022-07-03 14:34:46

J. Woolley

@Jonathon
With the following VBA, sNew will inherit the format of the first character of sOld. The remainder of the text box will retain its original format.

Sub TextBoxReplace()
    Dim shp As Shape
    Dim sOld As String
    Dim sNew As String
    Dim Compare, N
    Const CaseSensitive = vbBinaryCompare
    Const IgnoreCase = vbTextCompare

    'Change as desired
    sOld = "Old string"
    sNew = "New string"
    Compare = IgnoreCase
    
    For Each shp In ActiveSheet.Shapes
        N = 1
        With shp.TextFrame
            Do While N > 0
                N = InStr(N, .Characters.Text, sOld, Compare)
                If N > 0 Then
                    .Characters(N, Len(sOld)).Insert sNew
                    N = N + Len(sNew)
                End If
            Loop
        End With
    Next shp
End Sub

See https://sites.google.com/view/MyExcelToolbox/


2022-06-29 15:34:24

Jonathon

Thanks for all of the info. This is great.

I have text boxes that have a variety of formatting within them, different color/font/size.


When I run this macro, it changes all of the formatting to be whatever the format is for the first words of the text box. Is there a way that all of the formatting in the text box can be retained?


2021-10-15 17:06:53

J Stern

Steves comment below was helpful for nested text boxes.

My problem - other nested text boxes have a formula that aren't being replaced.

ex of text box formula = ='CL Offsites Trend Table'!AW$4

trying to do a mass "find and replace" of just "CL Offsites" with a different name and this solution wont update these.


2021-04-12 23:20:07

Steve

Thanks, Allen. This was a great tip. I found the "TextBoxReplace" macro has one limitation - it doesn't work with text box shapes that are nested inside groups. Apparently, Excel treats each group of shapes as a shape in itself. I looked online for how to handle that, and found an answer at "https://stackoverflow.com/questions/38314958/iterate-thought-all-the-even-if-grouped-shapes-in-a-sheet-excel-vba". Here is a modified routine that will check if each shape is a group, and if the shape in question is a group, it will loop through the subsidiary shapes within that group shape. Interestingly, it appears to handle nested groups just fine.

Sub TextBoxReplace()
Dim shp As Shape
Dim subshp As Shape
Dim sOld As String
Dim sNew As String

'Change as desired
sOld = "Old String"
sNew = "New String"

On Error Resume Next

For Each shp In ActiveSheet.Shapes
Debug.Print shp.Name

If shp.Type = msoGroup Then
For Each subshp In shp.GroupItems
Debug.Print Space(2) + subshp.Name
With subshp.TextFrame.Characters
.Text = Application.WorksheetFunction.Substitute( _
.Text, sOld, sNew)
End With
Next subshp
Else
With shp.TextFrame.Characters
.Text = Application.WorksheetFunction.Substitute( _
.Text, sOld, sNew)
End With
End If

Next

End Sub


2019-07-21 08:00:04

SteveJez

John,
If you just select the text box or shape you can format as much as you like, font, size, colour, alignment. The only draw back is the whole box or shape has the same formatting. If you need a different format for a title or the like, create another text box, format that as you like, align the text boxes & group them together. This works in Excel 2007 & 365 so presumably everything in between.

HTH


2019-07-20 15:25:48

John

Well Now - that's something I just learned - using a formula to place the text in a cell into a text box. After playing around with it for a while, I have found one problem - I cannot select the text in the text box to format it (size, colour, weight, font, etc.), and formaing the text in the source cell doesn't follow through to the representation in the text box. This is true whether I select the text within the source cell or the cell iteself for formating. The only way I have found, so far, is to use some of the tools on the Format tab on the ribbon, such as the word art gallery, and this provides no obvious way to change the size of the font. I haven't experimented with charts, to see if the same holds true there. My esperiments were done with Excel 2010 in Windows 10


This Site

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.

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.