Export charts from active sheet [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi team,

I am using this code to export charts from all sheets in active workbook.

I would like to convert the code to export charts only from active sheet.

Thank you.

VBA Code:
Sub ExportCharts()
    Dim WS As Excel.Worksheet
    Dim SaveToDirectory As String

    Dim objChrt As ChartObject
    Dim myChart As Chart
    
    user = Environ("Username")
    SaveToDirectory = "C:\Users\" & user & "\Desktop\"

    For Each WS In ActiveWorkbook.Worksheets
        WS.activate
        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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You have missing variable declarations (user, myFileName) and I believe the Index variable used in the Kill statement should be objChrt.Index. However, for the original request, following changes should work.

VBA Code:
Sub ExportCharts()
    Dim WS As Excel.Worksheet
    Dim SaveToDirectory As String

    Dim objChrt As ChartObject
    Dim myChart As Chart
    
    user = Environ("Username")
    SaveToDirectory = "C:\Users\" & user & "\Desktop\"

    'For Each WS In ActiveWorkbook.Worksheets
        'WS.activate
        Set WS = ActiveSheet
        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
 
Upvote 0

Forum statistics

Threads
1,223,812
Messages
6,174,777
Members
452,582
Latest member
ruby9c

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top