Export chart as image - with click of a button

前端 未结 2 2016
小蘑菇
小蘑菇 2020-12-01 13:05

I\'m trying to create a button that would export a chart in sheet \"Graphs\" as a jpeg file. This is the code I have, however it keeps on showing this error:

2条回答
  •  悲哀的现实
    2020-12-01 14:00

    Thanks, I needed this to extract all charts in an image and it's almost midnight. Minor changes to the code above did the trick.

    Sub ExportChart()
        Dim WS As Excel.Worksheet
        Dim SaveToDirectory As String
    
        Dim objChrt As ChartObject
        Dim myChart As Chart
    
        SaveToDirectory = ActiveWorkbook.Path & "\"
    
        For Each WS In ActiveWorkbook.Worksheets
            WS.Activate 'go there
            For Each objChrt In WS.ChartObjects
                objChrt.Activate
                Set myChart = objChrt.Chart
    
                myFileName = SaveToDirectory & WS.Name & "_" & objChrt.Index & ".png"
    
                On Error Resume Next
                Kill SaveToDirectory & WS.Name & Index & ".png"
                On Error GoTo 0
    
                myChart.Export Filename:=myFileName, Filtername:="PNG"
            Next
        Next
    
        MsgBox "OK"
    End Sub
    

提交回复
热议问题