Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Excel Chart With a Command Button

Status
Not open for further replies.

Taco

Programmer
Apr 1, 2001
32
US
I have a chart as an object in a worksheet and I want to add a command button to print just the chart. When I add the command button and view the VB code the Chart Object is nowhere to be found.

When I click the chart with the mouse and choose print this prints just the chart - this is what I want the command button to do.

Also, I would like to create another command button to print out the range of cells which are the source data for the chart which I want to print separately.
 
Have you used the macro recorder for the steps outlined above?
 
Events are enabled for chart sheets by default. Before you can use events with a Chart object that represents an embedded chart, you must create a new class module and declare an object of type Chart with events. For example, assume that a new class module is created and named "EventClassModule." The new class module contains the following code.

Public WithEvents myChartClass As Chart
After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for this object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.)

Before your procedures will run, however, you must connect the declared object in the class module with the embedded chart. You can do this by using the following code from any module.

Dim myClassModule As New EventClassModule

Sub InitializeChart()
Set myClassModule.myChartClass = _
Worksheets(1).ChartObjects(1).Chart
End Sub
After you run the InitializeChart procedure, the myChartClass object in the class module points to embedded chart one on worksheet one, and the event procedures in the class module will run when the events occur
 
Thanks to both of you. Both helped and I got it now. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top