Changing Chart Location in Office 2010

Office Quick Note banner

Programmatically Working with Office 2010 Charts: Learn how to change the location of a chart in Microsoft PowerPoint 2010.

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | VBA | Word 2010

In this article
Add a Chart to a PowerPoint Presentation
Add a Standard Module to a PowerPoint Presentation
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

Published:   March 2011

Provided by:    Frank Rice, Microsoft Corporation

Microsoft Office 2010 includes a powerful and versatile charting engine. You can enhance and manipulate charts programmatically with many different classes, objects, and properties. In this topic, you add a chart to a Microsoft PowerPoint 2010 presentation and then programmatically move the chart from one slide to another slide. To complete this task, you must do the following:

  • Add a Chart to a PowerPoint Presentation

  • Add a Standard Module to a PowerPoint Presentation

  • Add the Code to the Visual Basic Editor

  • Test the Solution

Add a Chart to a PowerPoint Presentation

In this task, you add a chart to a new PowerPoint presentation.

To add the chart

  1. Start PowerPoint.

  2. Remove both shapes from slide 1. To remove a shape, right-click it and then click Cut.

  3. On the Insert menu, click the Chart download arrow, select a chart type, and then click OK.

Add a Standard Module to a PowerPoint Presentation

In this task, you open the Visual Basic Editor, and then insert a standard module.

To add a standard module to a PowerPoint presentation

  1. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    Note

    If you do not see the Developer tab in PowerPoint 2010, click the File tab, and then click Options. In the categories pane, click Popular, select Show Developer tab in the Ribbon, and then click OK.

  2. On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.

Add the Code to the Visual Basic Editor

In this task, you add programming code that moves the chart from one slide to another.

To add code to the Visual Basic Editor

  1. In the Projects pane, click Module1.

  2. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub InteractWithChartLocation()
        Dim shp As Shape
        Dim cht As Chart
    
        ' If there isn't already a second slide, create one now:
        If ActivePresentation.Slides.Count < 2 Then
            ActivePresentation.Slides.Add 2, ppLayoutBlank
        End If
    
        ' Loop through all the shapes on the first slide, if they exist,
        ' looking for the first chart shape:
        For Each shp In ActivePresentation.Slides(1).Shapes
            If shp.Type = msoChart Then
                Exit For
            End If
        Next shp
    
        ' If you found a chart, keep going:
        If shp.Type = msoChart Then
            ' Cut the shape to the clipboard:
            shp.Cut
    
            Dim rng As ShapeRange
            Dim shpNew As Shape
    
            ' Paste the shape from the clipboard onto the second slide.
            ' This method returns a ShapeRange--you need a
            ' reference to the first pasted shape:
            Set rng = ActivePresentation.Slides(2).Shapes.Paste
            Set shpNew = rng.Item(1)
    
            ' Set the location of the new shape:
            shpNew.Top = 10
            shpNew.Left = 10
        End If
    End Sub 
    

Test the Solution

In this task, you run the VBA code and watch how it affects your chart. The best way to watch the interaction is to place the PowerPoint window next to the Visual Basic Editor window.

To step through the code

  1. Drag the Visual Basic Editor window to the right side of your monitor.

  2. Drag the PowerPoint window to the left side of your monitor and adjust both windows until you can see them both.

  3. Click the Visual Basic Editor window, place the cursor in the InteractWithChartLocation module, and then press the F8 key to step through the code line-by-line. You see the code create slide 2 and then move the chart from slide 1 to slide 2.

Next Steps