Written by Allen Wyatt (last updated November 6, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Don has multiple charts in a workbook. Each chart contains two text boxes with dates of the current data and dates that the chart was updated. Don would like to change all of the text in these chart text boxes with a macro but doesn't know the proper technique to perform the task.
This can, of course, be done with a macro, but you should understand that there is a way to do it without a macro. You can, if you'd like, tie what is shown in a text box to the contents of a cell. This means that you could make each of your text boxes "dynamic" so that all you need to do is change what is in the associated cell. That cell could contain a text value, a numeric value, a date, or a formula—it doesn't really matter.
Here's how to achieve this technique: Select the text box you want to associate with a cell by clicking on it a single time. The familiar handles should appear around the outside of the text box. Then, in the Formula bar, enter a cell reference. You can do this by entering it like this:
=$C$15
This ties the contents of the text box to whatever is in cell C15. You could also, if you prefer, delete whatever is in the Formula bar and, with the Formula bar active, click on the cell you want linked. The address for the cell should appear in the Formula bar. Press Enter and the task is done.
Now, type something into the associated cell. Whatever you type should appear immediately in the text box.
Before some potential macro-related answers can be provided, you'll need a bit of background concerning Excel's object model, which is accessible through VBA.
There are two types of charts that can be contained in a workbook: a chart sheet and an embedded chart. Chart sheets are represented by a Chart object and belong to the Charts collection. Embedded charts, on the other hand, are represented by a Chart object that belongs to the ChartObjects collection which, in turn, belongs to a worksheet object.
If you place a text box on an actual chart sheet, then it can be accessed in the following manner:
Sub FindTextBoxes1() Dim c As Chart Dim s As Shape Dim sMsg As String Dim sName As String For Each c In ActiveWorkbook.Charts For Each s In c.Shapes If s.Type = msoTextBox Then sMsg = s.TextFrame2.TextRange.Text sName = s.Name MsgBox "Text: " & sMsg, vbOKOnly, sName End If Next s Next c End Sub
This macro steps through each chart sheet and then through each text box on each chart sheet. Whatever is in each text box is then displayed in a message box. If you want to change what is contained in a message box, all you need to do is set the Text property of the TextRange object.
Text boxes on embedded charts are a different story. Why? Because it is possible that the text box isn't really part of the chart, but is part of the worksheet. In other words, the "parent" of the text box could either be the embedded chart or it could be the worksheet.
A good way to find out if the text box is part of the embedded chart is to create a similar macro to the one just presented. This one, however, can step through each embedded chart in each worksheet and find each shape (text box) within that chart.
Sub FindTextBoxes2() Dim w As Worksheet Dim c As ChartObject Dim s As Shape Dim sMsg As String Dim sName As String For Each w In ActiveWorkbook.Worksheets For Each c In w.ChartObjects For Each s In c.Chart.Shapes If s.Type = msoTextBox Then sMsg = "This text box is a child of the " sMsg = sMsg & "embedded chart" & vbCrLf sMsg = sMsg & "Text: " & s.TextFrame2.TextRange.Text sName = s.Name MsgBox sMsg, vbOKOnly, sName End If Next s Next c For Each s In w.Shapes If s.Type = msoTextBox Then sMsg = "This text box is a child of the " sMsg = sMsg & "worksheet" & vbCrLf sMsg = sMsg & "Text: " & s.TextFrame2.TextRange.Text sName = s.Name MsgBox sMsg, vbOKOnly, sName End If Next s Next w End Sub
When you run the macro, you'll see a message box for each text box that is really a child of the embedded charts. Then, you'll see a message box for any text boxes that are a child of the worksheet. As in the previous macro, this one checks to make sure that the Shape object is really a text box before displaying any information about it. This is particularly important when stepping through each shape in the worksheet, because any embedded charts on the sheet are also considered part of the Shapes collection.
As earlier, to change the text contained in a text box, all you need to do is change the Text property of the TextRange object.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13377) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
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!
Excel provides quite a bit of flexibility in how your chart looks. One of the least flexible places is in adjusting the ...
Discover MoreIf you need a number of charts in your workbook to all be the same size, it can be a bother to manually change each of ...
Discover MoreIf you need to create a chart that uses logarithmic values on both axes, it can be confusing how to get what you want. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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