Excel Macro Monday – Save An Excel Chart As An Image File


Happy Monday Excellers, welcome back to the next blog post in my 2019 #macromonday series. Today let’s write a quick and easy Excel VBA macro to save an Excel chart as an image file. So, this macro is useful if you copy and paste, or use the snipping tool. Or if you use some other method to copy your Excel charts.

Preparing To Write The Macro

First, you will need to open the Visual Basic Editor.  There are two ways to do this.  Either by hitting ALT +F11 or selecting the Developer Tab | Code Group | Visual Basic.  Both methods have the same result.   You then have a choice, you can either create a module to store your code either in your Personal Macro Workbook or in your current workbook.  What’s the difference?.  If you save the macro in your Personal Macro workbook it will be available for use in any of my Excel workbooks.  If you store it in the current workbook then use is restricted to that workbook.

In this example I want to store this macro in my Personal Macro workbook.

Learn More About Your Personal Macro Workbook (PMW)

If you want to read more about your Excel PMW then check out my blog posts below.

Macro Mondays -Creating and Updating Your Personal Macro Workbook

Why Is My Personal Macro Workbook Not Loading Automatically?

Macro Mondays – Create A Shortcut To Your Personal Excel Macro Workbook

Starting The Macro

We need to start off the macro by inserting a New Module.  Do this by selecting the Personal.xlsb workbook, then Insert Module.  Type Sub then the name of your macro.  In this example, I have called it Sub ChartExport. Notice that Excel will automatically enter the end text End Sub to end the Sub Routine.  We simply need to enter the rest of the code between these two lines.

[stextbox id=’info’]
Sub ChartExport()

End Sub
[/stextbox]

Use The Chart.Export Method

The Chart.Export VBA command exports a chart to a variety of bitmap file formats. I generally use the .PNG format for exporting these charts.

[stextbox id=’info’]
ActiveChart.Export
[/stextbox]

The parameters for this method include

Filename (a string). This is the name of the exported file. This is a required parameter. In this example, I have decided to name the default name of the active chart. The full file path is the default active workbook file path. It has the PNG extension.

FilterName (Object). This is the language-independent name of the graphics filter as it appears in the registry. This is an optional parameter. In this example it is PNG.

[stextbox id=’info’]
Filename:=ActiveWorkbook.path & “\” & ActiveChart.Name & “.png”, FilterName:=”PNG”
[/stextbox]

F

Ending The Macro

Once the chart has exported, the code finally ends with the End Sub piece of code.  This was already entered into the module for us when started the type the name of the macro.

[stextbox id=’info’]
End Sub
[/stextbox]

More Related #MacroMonday Blog Posts

Write A Macro To Copy Filtered Rows To A New Excel Workbook

VBA Sub and Function Procedures- Explanation and Examples – Macro Monday

If you want more tips then sign up to my monthly Newsletter where I share 3 Excel Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.

More Related #MacroMonday Blog Posts

Write A Macro To Copy Filtered Rows To A New Excel Workbook

VBA Sub and Function Procedures- Explanation and Examples – Macro Monday

If you want more tips then sign up to my monthly Newsletter where I share 3 Excel Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.

Likewise, if you want to see all of the blog posts in the Macro Mondays Series Click The Link Below

How To Excel At Excel – Macro Mondays Blog Posts.

Learn Excel Dashboard Course

Barbara

Barbara is an experienced data analyst with over 20 years of experience. Author of the How To Excel At Excel Top 50 Excel tips Ebook, YouTuber and Excel Blogger.

Recent Posts