Copy Paste Chart

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
When I step through the code below, it works fine, however when I run the code it seems to run forever and I get an error on the ActiveSheet.Paste line

Run-time error '1004': Paste method of worksheet class failed

VBA Code:
Sub Copy_Paste_Chart()
Worksheets("Sheet1").ChartObjects("Chart 1").CopyPicture xlScreen, xlPicture

Worksheets("Sheet2").Select
Cells(4, 3).Select
ActiveSheet.Paste

With ActiveSheet
  With .Shapes(.Shapes.Count)
    .Name = "chtName1"
  End With
End With

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Does this help?

VBA Code:
Sub Copy_Paste_Chart()

    Worksheets("Sheet1").ChartObjects("Chart 1").CopyPicture xlScreen, xlPicture
    
    With Worksheets("Sheet2")
        .Paste
        With .Shapes(.Shapes.Count)
            .Name = "chtName1"
            .Left = .Parent.Cells(4, 3).Left
            .Top = .Parent.Cells(4, 3).Top
        End With
        .Select 'optional
        .Cells(1).Select 'optional
    End With

End Sub
 
Upvote 0
Solution
Thanks.

Although I'm confused as the initial code I posted now works

Not sure whether it was an issue with my excel yesterday, I even created a new workbook and same thing happened. No idea what caused it.

Something similar happened when was trying to copy formatting from one chart to another but that now works

VBA Code:
Sub CopyFormatofCharts()
'Can Take Ages to Run with error
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartArea.Copy
    ActiveSheet.ChartObjects("Chart 2").Activate
    ActiveSheet.PasteSpecial Format:=2
End Sub
 
Upvote 0
It's probably a timing issue, that's why my code avoids having to select the worksheet and cell before pasting the chart. Did you try my code? If so, did it work?
 
Upvote 0
Yes. Both codes worked

Yours was better than mine ??
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top