Copying and pasting chart

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This is the code I use to copy a chart from one worksheet to another:

Code:
        Set ch = Sheet1.ChartObjects("Chart")
       
        ch.Copy
               
        Sheet2.Paste Destination:=Sheet2.Cells(1, 1)

The program crashes with the error message:

Code:
run-time error '1004':

Method 'Paste' of object'_worksheet' failed

I click Debug and it points to this line:

Code:
        Sheet2.Paste Destination:=Sheet2.Cells(1, 1)

but when I click F5 to continue, it runs until then end of the code. Normally if there's an error, it shouldn't let you continue.

Can someone explain what is wrong?

Thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi tiredofit. I think you need to either select or activate sheet 2 before you paste the chart to it. HTH, Dave
 
Upvote 0
Trial pasting to a range instead. Dave
Code:
Sheet2.Paste Destination:=Sheet2.Range("A1")
Still failed I'm afraid.

The behaviour is weird because once it crashes, I shouldn't be able to continue but I can.
 
Upvote 0
Google...
Not sure what's different other than setting the range first? Dave
 
Upvote 0
This code requires neither sheet to be active.

VBA Code:
Sheet1.ChartObjects("Chart").Copy
Sheet2.Paste
With Sheet2.ChartObjects(Sheet2.ChartObjects.Count)
    .Top = Sheet2.Range("A1").Top
    .Left = Sheet2.Range("A1").Left
End With

Note that Sheet1 and Sheet2 are not necessarily the sheet names showing on the worksheet tabs (though they might be), but they are the values listed for each sheet in the (Name) property in the VB Editor's Properties Pane.
 
Upvote 0
This code requires neither sheet to be active.

VBA Code:
Sheet1.ChartObjects("Chart").Copy
Sheet2.Paste
With Sheet2.ChartObjects(Sheet2.ChartObjects.Count)
    .Top = Sheet2.Range("A1").Top
    .Left = Sheet2.Range("A1").Left
End With

Note that Sheet1 and Sheet2 are not necessarily the sheet names showing on the worksheet tabs (though they might be), but they are the values listed for each sheet in the (Name) property in the VB Editor's Properties Pane.
Thanks for pointing this out.

My patience was diminishing and before I saw your response, I rewrote my code to add charts instead of copying from an existing one!

Nevertheless, this problem has enabled me to identify yet another Excel quirk, ie the fact you can press continue after a error is strange.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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