Excel 2010 VBA - Export Chart from Chart Sheet

2020-05-03 08:52发布

I'm struggling to find the correct syntax to export a chart from a chart sheet, not embedded in a worksheet. I used to have the chart embedded, but transferred it to it's own sheet at the request of the user and I can't seem to find anything to suit. Here is the code as it stood with an embedded chart, could someone give me a hint as to the syntax?

Public Sub ExportChart()

    Dim varChartObject As ChartObject
    Dim varChart As Chart
    Dim varFilename As String
    Dim varPath As String

    Set varChartObject = Sheets("Output Chart").ChartObjects(1)
    Set varChart = varChartObject.Chart
    varFilename = Format(ThisWorkbook.Sheets("Parameters").Range("C5").Value, "YYYYMMDD")
    varPath = "MyPath\" & Format(ThisWorkbook.Sheets("Parameters").Range("C5").Value, "MM. MMMM")

    On Error Resume Next
    Kill varPath & "\" & varFilename
    On Error GoTo 0

    varChart.Export Filename:=varPath & "\" & varFilename & ".png", Filtername:="PNG"

    Set varChartObject = Nothing
    Set varChart = Nothing

End Sub

Thanks in advance

1条回答
闹够了就滚
2楼-- · 2020-05-03 09:44

You only need this at the start:

Set varChart = Charts("chart sheet name")
查看更多
登录 后发表回答