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

by Allen Wyatt
(last updated April 19, 2014)

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9264) applies to Microsoft Excel 2007, 2010, and 2013. 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

Deleting a Toolbar

After a time you may not need one of the custom toolbars you've created. This tip explains how to free up resources in Excel ...

Discover More

Setting the AutoRecover Directory

When you are using Word, it normally saves temporary AutoRecover files that reflect the latest state of your document. If you ...

Discover More

Smushing Text Together

Word gives you control over how your text appears on the page. This includes adjusting how close letters are to each other ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Wildcards in 'Replace With' Text

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

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 for ...

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
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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. 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 6 - 3?

2015-03-02 17:21:17

Tonya

This is a great macro---thank you! Is there a way to keep the source formatting in the text that you are replacing? All of the text in my text box becomes bold font. Id prefer it to stay the same as what the text was before I replaced it.


2014-10-30 10:31:58

Bigger Don

@ Theresa

The trick is to put a "For Each WorkSheet...Next Worksheet" loop around the "For Each sh...Next" loop.

Try this. It also uses Input Boxes instead of hardcoding for sOld and sNew.

Sub TextBoxReplace()
Dim shp As Shape
Dim sOld As String
Dim sNew As String
Dim wks As Worksheet
'Change as desired
sOld = InputBox("Replace this...")
sNew = InputBox("with this...")
On Error Resume Next
For Each wks In ActiveSheet.Parent.Worksheets
For Each shp In wks.Shapes
With shp.TextFrame.Characters
.Text = Application.WorksheetFunction.Substitute( _
.Text, sOld, sNew)
End With
Next shp
Next wks
End Sub


2014-10-29 11:19:31

Theresa

For the TextBoxReplace code - how would you loop through the entire workbook with multiples sheets to perform the same function on each sheet rather than one at a time?


2014-10-24 10:29:35

Bigger Don

@Curt

Not sure why you are having an issue or what problem you're having.

I'll start with the assumption you are running the code as-is, below that I'll address what one change you may have tried might have caused it to not work.

I did notice that your "double space (" ")" in your post has only one space. (Unlikely cause, but I thought I would mention it because I've done such things.). Also, when I ran the code if there are three consecutive spaces and the result is two consecutive spaces. That means re-running should clear those, but it doesn't completely do the trick since there could be longer strings of spaces than 3.

There are two approaches you might try.

First, to remove excess spaces you can use the Application.Worksheet.Trim function. To do this, replace "Application.WorksheetFunction.Substitute().Text, sOld, sNew)" with "Application.WorksheetFunction.Trim(.Text)". Two notes: The VBA Trim() function won't suffice, you need the Worksheet Trim(). The Trim() function with also remove leading and trailing.

Second, you can loop-test the .Text to see if it still has and occurrences of sOld.

With shp.TextFrame.Characters
Do Until InStr(1, .Text, sOld) = 0
.Text = Application.WorksheetFunction.Substitute( _
.Text, sOld, sNew)
Loop
End With


2014-10-23 15:10:08

Curt

Hi,

Thanks for all the great macros I've been able to use on a daily basis!

I tried using TextBoxReplace to search for a double space (" ") in text boxes and replace with a single space (" "). I can't seem to get it to work. I've tried to figure it out, but don't have the skill. Any chance you could help?


2014-09-23 08:05:04

Sabin Simionescu

For groups as well:

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

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


2014-05-22 16:03:03

Zelda

You are a miracle worker!
Thank you so much for this VBA!
I've been searching for 4 hours and WOW.

Thank you once again :-)


2014-05-10 04:37:18

Ashok

Hi,
Pl note that each Rectangular shape is one below the other in order and each connected with a ARROW to next Rectangular shape.


2014-05-10 04:32:03

Ashok

Hi,

can you pl help me with a macro code for the below task in excel 2007.I am new to macros.

1.In Sheet1 I have 100+ Rectangular shapes each containing one text box containing Text
2.I want these Text of each box to be replaced with text from Sheet2 Range A1 to A100+ retaining Text Formating of Sheet2 text.
Appreciate your timely help.


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.