简体   繁体   English

使用vba迭代Excel中的所有文本(单元格,图表,文本框)

[英]Iterate through all text (in cells, charts, textboxes) in Excel using vba

I built a function to search for and replace specific text throughout an entire excel document. 我构建了一个函数来搜索和替换整个Excel文档中的特定文本。 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. 这是有效的,但很多文档都有文本框和其他地方的图表,我不知道如何在不知道其确切名称的情况下访问这些文件。基本上我想搜索excel文档中的每个字符串并使用我的ReplaceWord函数来替换单词。 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. 您可以使用VBE中的locals窗口查看cht变量的其他属性。 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Excel中,我想遍历两个下拉列表以使用VBA运行所有可能的组合 - In Excel, I want to iterate through two drop down lists to run all possible combinations using VBA 如何在Excel / VBA中的单元格范围内进行迭代? - How to iterate over a range of cells in Excel/VBA? 遍历一系列文本框 - Iterate through a series of textboxes 使用 VBA 遍历工作簿中的所有图表 - Loop through all charts in a workbook with VBA 循环浏览文件夹中所有Excel工作簿中的所有工作表,以更改所有单元格中的字体,字体大小和文本对齐方式 - Loop through all worksheets in all Excel workbooks in a folder to change the font, font size, and alignment of text in all cells 如何在 Excel 中使用 VBA 遍历所有列 - How to loop through all columns using VBA in Excel 遍历填充的行,根据条件匹配,使用 VBA 将值添加到 Excel 中另一个工作表中的特定单元格 - Iterate through populated rows, Match based on criteria, add value to specific cell in another sheet in Excel using VBA 如何使用VBA在Excel中迭代可变列长度范围 - How to iterate through a variable-column-length range in Excel using VBA Excel 循环遍历第 1 行中的所有填充单元格 - Excel Loop Through all filled cells in row 1 使用Excel VBA循环浏览文件夹中的.csv文件并将文件名复制到最后一列的单元格中 - Using Excel VBA to loop through .csv files in folder and copy the filename into cells in last column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM