Sie sind auf Seite 1von 5

4/7/2015

Creating charts images in Excel VBA

Excel VBA Programming


Home
Getting Started
8 part section >>
VBA Programming Variables
6 Part Section >>
Conditional Logic
9 part section >>
Strings and String Functions
8 Part Section >>
Programming Loops
4 Part Section >>
Programming Arrays
4 Part Section >>
Subs and Functions
6 Part Section >>
Excel VBA and Text Files
2 Part Section >>
Excel VBA and User Forms
5 part section >>
An Excel Picture Viewer Project
6 Part Section
Excel VBA and Charts
Chart Sheets
Embedded Charts
Charts and User Forms, Part One
Charts and User Forms, Part Two
Reference: Chart Constants
A TreeView Project
A 6 part section >>

http://www.homeandlearn.org/create_vba_chart_image.html

1/5

4/7/2015

Creating charts images in Excel VBA

#1 OPC
Excel Addin
OPC Excel
Plug-in OPCDA, HDA. Free
Download.

Excel Chart Images


This is part two of the tutorial on charts and User Forms. Part One is here: Charts and User Forms.

IT Service Desk Software


Prioritize & Manage Your Work Load. Learn How with a Free Trial Now!

In the previous section, we created a chart from data on a spreadsheet. Now that we have a chart, we
can create an image from it. First, we need a file name and location to save the chart. Add these two
lines to your code:
Dim imageName As String
imageName = Application.DefaultFilePath & Application.PathSeparator &
"TempChart.gif"
The fileName variable is a string. To get a location to save the file, you can use
Application.DefaultFilePath. The default file path is usually the Documents folder in Windows. You
can check this location for yourself by adding a message box:
MsgBox "The default file path is " & Application.DefaultFilePath
This will tell you where on your computer Excel is going to save the image of the chart. The
Application.PathSeparator part just gets you a backslash character ("\"). At the end, you can then
type a name for your file. Ours is "TempChart.gif". As well as the GIF format, you can save your
images as a JPEG file, or a PNG file. If you want to keep the size of the image file down, though, then
use GIF or PNG.
To actually save a file, you need the Export method
http://www.homeandlearn.org/create_vba_chart_image.html

2/5

4/7/2015

Creating charts images in Excel VBA

MyChart.Export FileName:= imageName, FilterName:="GIF"


After a space, can have up to three parameters:
FileName - The name of your file
FilterName - An export filter (you get graphics filters when you install Excel/Microsoft Office)
Interactive - Either True or False. (Supposed to display a dialogue box with graphic filters,
but doesn't seem to work. You can ignore this parameter.)
We're just using the first two parameters, FileName and FilterName. We're setting FileName to be
whatever is in the variable imageName. The FilterName is just whatever format you want to save your
image as. If you want to create a JPEG image then change the filter name to JPEG.
When the Export command is run, Excel will save the file for you. If a file of that name already exists
then it is overwritten, which is exactly what we want. Nothing else is needed here.
The next step is to delete the chart on the spreadsheet: We need to do this so that we don't have a lot
of chart objects embedded in the spreadsheet. The process is quite easy:
ActiveSheet.ChartObjects(1).Delete
All we're doing here is to using the Delete method of the ChartObjects collection. The number
between the round brackets is which number chart you want to delete.
Only two more lines to go, now. The first of these is to switch back on ScreenUpdating:
Application.ScreenUpdating = True
The last thing we need to do is to load the saved chart into our Image control:
UserForm1.Image1.Picture = LoadPicture(imageName)
The Image control has a property called Picture. After an equal sign we're then using the
LoadPicture method:
LoadPicture(imageName)
In between the round brackets of LoadPicture you type a file path for the image you want to load into
your image control. For us, this is contained in the variable called imageName.
And that's it. The whole of your code should look like this:

http://www.homeandlearn.org/create_vba_chart_image.html

3/5

4/7/2015

Creating charts images in Excel VBA

Test it out. Run your form and select the Arsenal item from the dropdown list. Then click the Load
Chart button. You should see this:

http://www.homeandlearn.org/create_vba_chart_image.html

4/5

4/7/2015

Creating charts images in Excel VBA

Now select the Newcastle item from the dropdown list. The chart will change to this:

You can quickly see the difference in data as each chart is displayed. And all from a dropdown list on
a user form!

Lots more free online course here on our main Home and Learn site
All course material copyright Ken Carney

http://www.homeandlearn.org/create_vba_chart_image.html

5/5

Das könnte Ihnen auch gefallen