图表中文本框的公式 - 如何在不选择的情况下引用它?

问题描述 投票:0回答:2

我有以下有效代码

Sub TestMe()
Dim crt As ChartObject
Dim shp_Shape As Shape
Dim chartX As Chart

For Each crt In ActiveSheet.ChartObjects

    Set chartX = crt.Chart
    For Each shp_Shape In chartX.Shapes
        If shp_Shape.Type = msoTextBox Then
            shp_Shape.Select
            MsgBox (Selection.Formula)
        End If

    Next
Next

End Sub

我想做的是简化代码,这样我就不必选择 shp_Shape 对象。 像 msgbox(shp_Shape.Formula) 这样的东西

我希望能够阅读这个公式,而无需实际选择任何内容。 这样,即使工作表被隐藏或图表/工作表受到保护,我也可以获得该属性。

有什么想法吗?

vba excel charts textbox excel-formula
2个回答
2
投票

msoTextBox
形状没有
.Formula
属性。你需要的是
TextFrame2.TextRange.Text

试试这个

Sub TestMe()
    Dim crt As ChartObject
    Dim shp_Shape As Shape
    Dim chartX As Chart

    For Each crt In ActiveSheet.ChartObjects
        Set chartX = crt.Chart
        For Each shp_Shape In chartX.Shapes
            If shp_Shape.Type = msoTextBox Then
                MsgBox shp_Shape.TextFrame2.TextRange.Text
            End If
        Next
    Next
End Sub

屏幕截图

enter image description here

后续评论

如果您的

msoTextBox
形状与公式链接,那么您需要以下

shp_Shape.DrawingObject.Formula

0
投票

重要提示:对于文本框中的公式,您也可以通过 VBA 添加它,但如果这样做,不要以等号开头。如果您确实添加它,您将收到错误 1003。这当然不直观,因为它与 Excel 单元格相反。

© www.soinside.com 2019 - 2024. All rights reserved.