似乎您将不得不迭代图表的属性。设置变量后,您可以使用 VBE 中的局部变量窗口查看cht
变量的其他属性。这不是一个详尽的选项列表,但应该足以让您入门!
Sub ReplaceTextInChart()
Dim cObj As ChartObject
Dim cht As Chart
Dim ax As Axis
Dim legEnt As LegendEntry
Dim srs As Series
Dim str As String 'this variable will use to hold the various text of the chart.'
Dim strSearch As String
Dim strReplace As String
strSearch = "s" '<-- test that I used, modify as needed.'
strReplace = "##" '<-- test that I used, modify as needed.'
For Each cObj In ActiveSheet.ChartObjects
Set cht = cObj.Chart
With cht
'## Check if the chart has a title, if so, do the replace.'
If .HasTitle Then
str = .ChartTitle.Characters.Text
.ChartTitle = Replace(.ChartTitle, strSearch, strReplace)
End If
'## Check if the chart has a legend, if so, do the replace'
If .HasLegend Then
For Each legEnt In .Legend.LegendEntries
str = legEnt.Format.TextFrame2.TextRange.Characters.Text
legEnt.Format.TextFrame2.TextRange.Characters.Text = _
Replace(str, strSearch, strReplace)
Next
End If
For Each ax In .Axes
'## Check if each Axis has a Title, if so, do the replace'
If ax.HasTitle Then
str = ax.AxisTitle.Characters.Text
ax.AxisTitle.Characters.Text = Replace(str, strSearch, strReplace)
End If
Next
'## For each series, do the replace in series.name'
For Each srs In .SeriesCollection
str = srs.Name
srs.Name = Replace(str, strSearch, strReplace)
Next
End With
Next
End Sub