如何通过 excel 宏向图表添加文本?

How to add text to a chart by excel macro?

使用以下代码通过 excel 宏向图表添加文本? 当编译成模块时,它显示 "object doesn't support this property or method",非常感谢任何建议或修改代码。谢谢

Set myDocument = Ch.Chart

  myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 382, 266, 122, 20).Select

    Selection.ShapeRange.TextFrame.Characters.Text = ThisWorkbook.ActiveSheet.Cells(6, 8)

   With Selection.ShapeRange.TextFrame.Characters

    .Font.Name = "Tahoma"

    .Font.Size = 10

    .Font.Bold = msoTrue

   End With

在添加文本框等之前不需要 select 图表。您的代码可以重写如下...

Dim theChartObj As ChartObject
Set theChartObj = Worksheets("Sheet1").ChartObjects("Chart 1") 'change the sheet and chart names accordingly

Dim theChart As Chart
Set theChart = theChartObj.Chart

Dim theTextBox As Shape
Set theTextBox = theChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 382, 266, 122, 20)

With theTextBox.TextFrame.Characters
    .Text = ThisWorkbook.ActiveSheet.Cells(6, 8).Value
    With .Font
        .Name = "Tahoma"
        .Size = 10
        .Bold = msoTrue
    End With
End With