请参阅以编程方式添加的图表

时间:2016-10-04 14:06:52

标签: vba excel-vba excel

我使用以下代码插入图表。我现在如何参考上述图表?我知道我应该使用ChartObject,但是如何设置它?

更具体地说,我想将图表分配给变量,但我不确定如何做到这一点。我试过了:

set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 420, , 175)

但无济于事。

Sub InsertInfection(rngToPrint As Range, lngTopLeft As String, BottomLeft As String)
    Dim strRange As String
    Dim rngChart As Range
    Dim myChart As ChartObject

    lngStartRow = Sheets(rngToPrint.Worksheet.Name).Range(lngTopLeft).Row
    lngEndRow = Sheets(rngToPrint.Worksheet.Name).Range(BottomLeft).Row

    Sheets(rngToPrint.Worksheet.Name).Activate
    Sheets(rngToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$D$" & CStr(lngEndRow)).Select

    ActiveSheet.Shapes.AddChart(xlLine, 500, 420, , 175).Select

End Sub

1 个答案:

答案 0 :(得分:2)

喜欢这个

Dim myChart as Chart
set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 420, , 175).Chart

如果您需要ChartObject的引用,请使用myChart.Parent