2013-04-26 2 views
0

전체 Excel 문서에서 특정 텍스트를 검색하고 바꿀 수있는 기능을 만들었습니다. 나는 전체 문서를 통해 이동하고 모든 세포를 잘vba를 사용하여 Excel에서 셀, 차트, 텍스트 상자의 모든 텍스트를 반복합니다.

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 –

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 
관련 문제