Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to find and replace text in chart titles in Excel?

Charts are very useful for us in Excel, and we often give them some titles for identifying them. But have you tried to find and replace the text value in chart titles within multiple charts? Today, I will talk about how to find a specific text in multiple chart tiles and replace them with other value in Excel.

Find and replace text in chart titles in Excel with VBA code


arrow blue right bubble Find and replace text in chart titles in Excel with VBA code


Supposing you have a worksheet which contains the charts as following screenshot shown, and you want to replace the text January by February within all of the chart titles only. Of course, you can change manually them one by one, but this will be time-consuming if there are multiple charts. So here, the following VBA code can help you to deal with this task.< /p>

doc-replace-chart-titles1

1. Activate your worksheet which contains the charts you want to find and replace in their titles.

2. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

3. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Find and replace text in chart titles within active worksheet

Sub ChartLabelReplace()
'Update 20140603
Dim xWs As Worksheet
Dim xFindStr As String
Dim xReplace As String
xFindStr = Application.InputBox("Find:", xTitleId, "", Type:=2)
xReplace = Application.InputBox("Replace:", xTitleId, "", Type:=2)
Set xWs = Application.ActiveSheet
For Each ch In xWs.ChartObjects
    If ch.Chart.HasTitle Then
        ch.Chart.ChartTitle.Text = VBA.Replace(ch.Chart.ChartTitle.Text, xFindStr, xReplace, 1)
    End If
Next
End Sub

4. Then press F5 key to run this code, and a prompt box will pop out to remind you entering your old text that you want to replace, see screenshot:

doc-replace-chart-titles1

5. And then click OK button, and another prompt box pops up, you need to type your new text value that you want to replace the old one, see screenshot:

doc-replace-chart-titles1

6. Go on clicking OK to close the prompt box, and your old texts have been replaced by the news at one time in the current worksheet as following screenshot shown:

doc-replace-chart-titles1

Note: If you need to find and replace the text value of chart titles in all of the worksheets in a workbook, you should apply the following VBA code: (The procedure is the same as the above)

VBA code: Find and replace text in chart titles within all worksheets

Sub ChartLabelReplaceAllWorksheet()
'Update 20140603
Dim xFindStr As String
Dim xReplace As String
xFindStr = Application.InputBox("Find:", xTitleId, "", Type:=2)
xReplace = Application.InputBox("Replace:", xTitleId, "", Type:=2)
For Each sh In Worksheets
    For Each ch In sh.ChartObjects
        If ch.Chart.HasTitle Then
            ch.Chart.ChartTitle.Text = VBA.Replace(ch.Chart.ChartTitle.Text, xFindStr, xReplace, 1)
        End If
    Next
Next
End Sub

Related articles:

How to find and replace text within comments in Excel?

How to find and replace specific text in multiple text boxes of Excel?

How to change multiple hyperlink paths at once in Excel?


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!

 

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Tony D · 2 years ago
    I want to do this for text in the legend and axis labels as well. What's the VBA name for the Legend? I tried to edit the module by replacing "ChartTitle" with "Legend" "LegendEntry" "ChartLegend" none work.
  • To post as a guest, your comment is unpublished.
    Drew · 3 years ago
    Some of my chart titles contain multiple font sizes. When I run the macro above, it converts all of the fonts to the same size and I have to go back through and manually re-size them. Is there a way to modify the VBA code so that it maintains the original font sizes? (I'm still a VBA newb, so any help would be appreciated!)
  • To post as a guest, your comment is unpublished.
    Natty · 3 years ago
    Thank you!!! It helped me so much!!!
  • To post as a guest, your comment is unpublished.
    SJ · 4 years ago
    This one works for me, because otherwise the variables are not declared :

    Sub ChartLabelReplace()
    'Update 20141017
    Dim xFindStr As String
    Dim xReplace As String
    Dim ws As Worksheet
    Dim ch As ChartObject
    xFindStr = InputBox("Find:")
    xReplace = InputBox("Replace:")
    For Each ws In Worksheets
    For Each ch In ws.ChartObjects
    If ch.Chart.HasTitle Then
    ch.Chart.ChartTitle.Text = VBA.Replace(ch.Chart.ChartTitle.Text, xFindStr, xReplace, 1)
    End If
    Next
    Next
    End Sub
    • To post as a guest, your comment is unpublished.
      CB · 2 years ago
      How does one change the VBA macro to only apply the change to selected charts (and not all the charts on the worksheet)?