cellMatrix.net

Spreadsheet Modeling and Related Topics

Copy Embedded Charts as Shapes with VBA

Andy Pope recently posted a nice bit of code in response to a question posted in the Microsoft Excel Charting Discussion Group. The question asked how to copy a chart as a shape without using a selection event. I've struggled with this same question. Specifically, how do you to copy charts as shapes across different sheets without selecting the destination sheet. Andy's example does the trick:

Sub CopyChartsAsShapes()

    Application.ScreenUpdating = False

    Dim Cht As Chart
    Dim Rng As Range

    Set Cht = Sheets("MC").ChartObjects("Cht1").Chart

    For Each Rng In Sheets("Dash").Range("F4:F11")

        Cht.CopyPicture
        With Rng.Parent
            .Paste
            With .Shapes(.Shapes.Count)
                .LockAspectRatio = msoFalse
                .Left = Rng.Left
                .Top = Rng.Top
                .Width = Rng.Width
                .Height = Rng.Height
            End With
        End With

    Next Rng

    Cht.Deselect
    
    Application.ScreenUpdating = True

End Sub

In the example, the procedure copies a chart that resides on the sheet called "MC" to each cell within the range F4:F11 on the sheet called "Dash". The poster had named the chart "Cht1", which can easily be done by activating, or clicking on the embedded chart and then running the macro below:

Sub NameChart()
    ActiveChart.Parent.Name = "Cht1"
End Sub
Posted on Wednesday, January 07, 2009 | Comments (0) | Permalink

Comment Entry Form

User Information

Name: (Required)

E-Mail Address: (Optional)

Location: (Optional)

Web Site Address: (Optional)

Comment Formatting Reference
Add VBA Code:
<pre>some code here</pre>
Linking:
[url]http://www.example.com/[/url]
[url=http://www.example.com/]my site[/url]
Text Formatting
[size=4]Some larger text[/size]
[color=green]Some green text[/color]
[b]Some bold text[/b]
[i]Some italic text[/i]
Enter Comment Below

Personalization Options
Remember my personal information
Notify me of follow-up comments?

Next entry: Minor Bar Chart Annoyance

Previous entry: Floating Bars with Min, Max, and Averages

<< Back to main

Statistics

  • Total Entries - 136
  • Current Viewers - 11

Categories

Entries by Day

Sep - 2010
S M T W T F S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2

Recent Comments

Syndicate