by Allen Wyatt
(last updated October 29, 2018)
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:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13377) applies to Microsoft Excel 2007, 2010, and 2013.
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!
Excel's charts are normally created in color, but you can print them in black and white. You may be looking for a way to ...Discover More
One way you can make your charts look more understandable is by removing the "jaggies" that are inherent to line charts. ...Discover More
When creating a chart, your axis labels may be wider than desired. One way to deal with them is to change the angle at ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.