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

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

我有一个 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

我期望绿色框与橙色图表区域的左上角齐平,但正如您所看到的,在上/左零坐标开始之前似乎有某种边距..

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

vba excel charts position
1个回答
2
投票

尝试使用

IncrementLeft
IncrementTop

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
© www.soinside.com 2019 - 2024. All rights reserved.