Microsoft Office Tutorials and References
In Depth Information
Displaying a Chart in a UserForm
Displaying a Chart in a UserForm
Oddly, Excel provides no direct way to display a chart in a UserForm. You can, of course, copy
the chart and paste it to the Picture property of an Image control, but this creates a static
image of the chart, so it won’t display any changes that are made to the chart.
This section describes a technique to display a chart in a UserForm. Figure 15-25 shows a
UserForm with a chart displayed in an Image object. The chart actually resides on a worksheet,
and the UserForm always displays the current chart. This technique works by copying the chart
to a temporary graphics file and then using the LoadPicture function to specify that file for
the Image control’s Picture property.
This workbook is available on the companion CD-ROM. The filename is chart in
userform.xlsm .
To display a chart in a UserForm, follow these general steps:
1.
Create your chart or charts as usual.
2.
Insert a UserForm and then add an Image control.
3.
Write VBA code to save the chart as a GIF file and then set the Image control’s
Picture property to the GIF file.
You need to use VBA’s LoadPicture function to do this.
4.
Add other bells and whistles as desired.
For example, the UserForm in the demo file contains controls that let you change the
chart type. Alternatively, you could write code to display multiple charts.
Figure 15-25: With a bit of trickery, a UserForm can display “live” charts.
 
Search JabSto ::




Custom Search