Excel VBA图表自动形状左上角位置

时间:2016-12-09 11:13:30

标签: excel vba excel-vba charts position

我在Excel中有一个图表。我在图表中添加了一个形状:

excelChart.Shapes.AddShape(msoShapeRectangle, 0, 0, excelChart.ChartArea.width, 15)

Chart with shape

我选择了绿色框并运行了下面的宏:

Sub Macro6()

    Selection.ShapeRange.Left = 0
    Selection.ShapeRange.Top = 0

End Sub

我原本期望绿色框在橙色图表区域的左上方齐平,但正如您所看到的那样,在顶部/左侧零坐标开始之前似乎存在某种边距..

如何以编程方式将绿色框放在角落里?

1 个答案:

答案 0 :(得分:1)

尝试使用IncrementLeftIncrementTop

Sub AddBox()
    Dim cht As Chart

    Set cht = Worksheets(1).ChartObjects(1).Chart

    With cht.Shapes.AddShape(msoShapeRectangle, 0, 0, cht.ChartArea.Width, 15)
        .Name = "MyShape"
        .IncrementLeft -5  //Experiment with number to get desired effect
        .IncrementTop -5
    End With
End Sub