Diagram Object

You can illustrate various concepts using diagramming tools in Excel 2002. A diagram is a collection of shapes. Diagram types include Cycle, Target, Radial, Venn, and Pyramid.

Each diagram contains nodes. A node represents an individual shape object in a diagram. To add a Diagram object programmatically to a worksheet, use the AddDiagram method of the Shapes collection. To add shapes to the diagram, use the DiagramNode object within the Shape object. The following procedure creates a diagram with four nodes. A problem in Microsoft Excel 2002 makes it impossible to programmatically set the text in a diagram node. Microsoft has posted a workaround to this problem, where you can use Microsoft Word or PowerPoint to create a diagram and then copy it to Excel (see article Q317293 in the Microsoft Knowledge Base for an example VBA procedure).

Sub AddADiagram()

Dim dgnNode As DiagramNode Dim shpDiagram As Shape Dim intNodes As Integer


' Add diagram and first child node Set shpDiagram = ActiveSheet.Shapes.AddDiagram _ (Type:=msoDiagramCycle, Left:=10, Top:=15, _ Width:=400, Height:=475) Set dgnNode = shpDiagram.DiagramNode.Children.AddNode

' Add three additional child nodes For intNodes = 1 To 3

dgnNode.AddNode Next intNodes

' Automatically format the diagram dgnNode.Diagram.AutoFormat = msoTrue

' Change the diagram type dgnNode.Diagram.Convert Type:=msoDiagramRadial

' Display the number of nodes in the diagram

MsgBox dgnNode.Diagram.Nodes.Count ' Select all nodes

ActiveSheet.Shapes(1).Diagram.Nodes.SelectAll End Sub

0 0

Post a comment