VBA for Excel 2007 tutorial-chart saved in image format

Chart saved in image format

Sometimes, you want to use your chart generated in MS Excel in other documents let say in MS Word or in your web site. It is a good idea to save the chart in an image format(e.g. gif, jpg, bmp, png,...ect.). This task can done by only a single line of VBA code.

VBA for Excel: VBA example code save chart in image format

To run this VBA example code, you can copy and paste the VBA code below in to the block of Worksheet_SelectionChange() procedure. To open this block, double-click the current worksheet, then in the objects dropdown list select WorkSheet and in events list select SelectionChange.

VBA code for this example:

Option Explicit
Private Sub Worksheet_Activate()
    'Select the data source
    'Create a pie chart
    ActiveSheet.Shapes.AddChart , 10, 100
    ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$G$2"), PlotBy:=xlRows   

    'Set chart title
    ActiveChart.HasTitle = True
    ActiveChart.ChartTitle.Caption = "Coca Cola Sale by Year"
    'Specify Pie chart type
    ActiveChart.ChartType = xl3DPie
    'Change the chart type
    ActiveChart.ChartType = xl3DPieExploded

    'Display data values and percentages
    ActiveChart.ApplyDataLabels xlDataLabelsShowValue, , , , , , , True
    ActiveChart.Export "test.gif", "GIF" 'Create gif image file
End Sub 


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.