Beruflich Dokumente
Kultur Dokumente
http://www.homeandlearn.org/create_vba_chart_image.html
1/5
4/7/2015
#1 OPC
Excel Addin
OPC Excel
Plug-in OPCDA, HDA. Free
Download.
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
http://www.homeandlearn.org/create_vba_chart_image.html
3/5
4/7/2015
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
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