2013-04-26 48 views
0

我構建了一個函數來搜索和替換整個excel文檔中的特定文本。我可以在整個文檔及其所有細胞就好使用使用vba在Excel中遍歷所有文本(在單元格,圖表,文本框中)使用vba

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 

這工作,但很多的文件有圖表,在文本框和其他地方的標題,我不知道如何訪問這些不知道他們的確切名稱等。 基本上我想在excel文檔中搜索每一個字符串,並使用我的ReplaceWord函數替換單詞。但我失去了如何:)

任何幫助將不勝感激。謝謝!

回答

1

看來你將不得不遍歷圖表的屬性。一旦設置完畢,您可以使用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 
+1

我認爲兩者之間我們這將是非常有益的。 +1 – 2013-04-26 16:11:24

1

這本書以形狀,包括文本框,並列入表,以及對自己的表圖圖表:

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 
相關問題