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.
Written by Allen Wyatt (last updated June 6, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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:
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.
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!
Excel's Find and Replace capabilities are handy, but they aren't as full-featured as those in Word. One shortcoming is ...
Discover MoreExcel provides some great tools for finding information in a worksheet or a workbook. Changing the default settings used ...
Discover MoreIf you use a worksheet that contains dates, you might want to search for those dates at some point. This isn't as simple ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments