I built a function to search for and replace specific text throughout an entire excel document. I can go through the entire document and all its cells just fine using
For Each WS In Worksheets
For Each ActiveCell In WS.UsedRange
If ActiveCell <> "" Then
ActiveCell.Value = ReplaceWord(ActiveCell.Value, Search, Replacement)
End If
next
next
This works, but a lot of the documents have charts with titles in textboxes and other places and I am not sure how to access those without knowing their exact names etc. Basically I would like to do a search for every single string in the excel document and use my ReplaceWord function to replace the words. But I am lost on how :)
Any help would be much appreciated. Thanks!
It seems like you will have to iterate over the properties of the chart. You can use the locals window in the VBE to view other properties of the cht
variable once it's been set. This is not an exhaustive list of options, but it should be enough to get you started!
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
This deals with shapes, including textboxes, and charts included in sheets, as well as charts on their own sheets:
Sub ReplaceTextInShapesAndCharts()
Dim ws As Excel.Worksheet
Dim chtObject As Excel.ChartObject
Dim chtChart As Excel.Chart
Dim shp As Excel.Shape
For Each ws In ThisWorkbook.Worksheets
'textboxes and other shapes
For Each shp In ws.Shapes
'charts don't have TextFrames - handled separately
If Not shp.Type = msoChart Then
shp.TextFrame.Characters.Text = Replace(shp.TextFrame.Characters.Text, "great", "fantastic")
End If
Next shp
'in-sheet charts
For Each chtObject In ws.ChartObjects
ChartTextReplace chtObject.Chart
Next chtObject
'charts on their own sheets
For Each chtChart In ThisWorkbook.Charts
ChartTextReplace chtChart
Next chtChart
Next ws
End Sub
Sub ChartTextReplace(chtChart As Excel.Chart)
Dim shp As Excel.Shape
With chtChart
'textboxes in chart
For Each shp In .Shapes
shp.TextFrame.Characters.Text = Replace(shp.TextFrame.Characters.Text, "great", "fantastic")
Next shp
'expand this section as needed
.ChartTitle.Text = Replace(.ChartTitle.Text, "great", "fantastic")
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.