Diagram Object

Microsoft Excel Visual Basic

Diagram Object

Multiple objects Diagram
DiagramNodes

Represents a diagram.

Using the Diagram object

Use the Diagram property of the Shape object or ShapeRange collection to a return a Diagram object. The following example adds a radial diagram to the active worksheet.

Sub NewDiagram()

    Dim wksActiveSheet As Worksheet
    Dim shDiagram As Shape

    Set wksActiveSheet = ActiveSheet
    Set shDiagram = wksActiveSheet.Shapes.AddDiagram( _
        Type:=msoDiagramRadial, _
        Left:=20, Top:=40, _
        Width:=400, Height:=200)

    ' Fill the diagram to make it visible to the user
    shDiagram.Fill.Visible = msoTrue

End Sub
		

You can also convert the current diagram to a different diagram by using the Convert method.

Note   If the current diagram is an organization chart (msoDiagramOrgChart) a run-time error will occur. In this example, a radial diagram is converted into a target diagram.

Sub NewDiagram()

    Dim wksActiveSheet As Worksheet
    Dim shDiagram As Shape

    Set wksActiveSheet = ActiveSheet
    Set shDiagram = wksActiveSheet.Shapes.AddDiagram( _
        Type:=msoDiagramRadial, _
        Left:=20, Top:=40, _
        Width:=400, Height:=200)

    ' Fill the diagram to make it visible to the user
    shDiagram.Fill.Visible = msoTrue

    ' Convert the diagram.
    shDiagram.Diagram.Convert Type:=msoDiagramTarget

End Sub
		

There are several types of diagrams to chose from when working with the Diagram object. Refer to the AddDiagram method to view a list of available diagram types.