使用VBA更改图表的位置

时间:2017-06-21 13:40:18

标签: excel vba excel-vba

我正在尝试使用我的vba更改生成的图形的位置。目前,它只是从列中获取可能会或可能不会更改大小的数据。我知道我没有' Chart1'确定离子我的代码,但我无法弄清楚在哪里宣布它不会为图表创建另一张纸。

reportsheet.Select
ActiveSheet.Range("a4", ActiveSheet.Range("a4").End(xlDown)).Select
ActiveSheet.Shapes.AddChart.Select

With ActiveSheet.Shapes("Chart1")
   .Left = Range("A40").Left
   .Top = Range("A40").Top
End With

2 个答案:

答案 0 :(得分:0)

您可以更改活动图表的名称,然后为其指定属性。

试试这个...

    reportsheet.Select
    ActiveSheet.Range("A4", ActiveSheet.Range("A4").End(xlDown)).Select
    ActiveSheet.Shapes.AddChart.Select

    ActiveChart.Parent.Name = "Chart1"

    With ActiveSheet.Shapes("Chart1")
       .Left = Range("A40").Left
       .Top = Range("A40").Top
    End With

答案 1 :(得分:0)

我像这样使用make chart。请参考风箱。

Sub test()
    Dim Ws As Worksheet
    Set Ws = ActiveSheet

    InsertCharts 20, Ws
    InsertCharts 30, Sheets("Sheet1")
End Sub

Sub InsertCharts(n As Integer, Ws As Worksheet)

    Dim Cht As Shape
    Dim t As Single, w As Integer, h As Integer, x As Integer
    Dim i As Integer

    With Ws
        If .ChartObjects.Count > 0 Then
            .ChartObjects.Delete
        End If

        x = 0
        t = .Range("a26").Top
        w = 217.1338582677
        h = 203.5275590551
        For i = 1 To n

                Set Cht = .Shapes.AddChart(, x, t, w, h)
            If i Mod 5 = 0 Then
                t = .Range("a26").Top
                x = x + w + 20
            Else
                t = t + h + 20
            End If

        Next i
    End With
End Sub