Excel 2010 VBA - Export Chart from Chart Sheet

白昼怎懂夜的黑 提交于 2019-12-20 07:06:48

问题


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:


You only need this at the start:

Set varChart = Charts("chart sheet name")


来源:https://stackoverflow.com/questions/34789713/excel-2010-vba-export-chart-from-chart-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!