Macro occasionally crashes when pasting a chart object

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a complex macro that imports data into a master file and creates output files.

Part of the requirement is for the output files to have 3 interactive graphs linked to pivot tables also created in the output files. The Master file contains the chart object that I copy and paste into the output file before breaking links and linking to the data in the output file only.

The macro creates 5 or 6 output files, depending on the input data. I find when the macro first runs, it creates all output files correctly. However, if I try to run it again (and the same problem occurs on another User's PC, though on their first rather than subsequent attempt), it crashes at the point of pasting the chart object.

This is the code including line (red) it errors on, it is always on this line (i.e. after Chart1.Copy). I know it's not the full code:
Rich (BB code):
            Application.CutCopyMode = False

            oChart1.Copy
            'wkb.Activate
            With Sheets(PvtTable4)
                .Select
                ActiveSheet.Paste
            End With
            
            Application.CutCopyMode = False
            
            oChart2.Copy
            'wkb.Activate
            With Sheets(PvtTable5)
                .Select
                ActiveSheet.Paste
                oChart3.Copy
                ActiveSheet.Paste
            End With
                                    
            Application.CutCopyMode = False

This is called from a loop in another procedure (from another module), the variables being passed are correct. As explained, it runs fine for the first two generated output files (i.e. for the first 2 loop iterations), but then errors on the highlighted line. However, this doesn't happen every time I run the macro, usually it occurs after the first attempt. I am also using Application.CutCopyMode = False to clear the clipboard before anything is copied and after each paste.

Apart from writing code to generate the initial chart for the Master file (rather than use the existing chart in the Master file) and then re-use that script to generate charts for the output files, can anyone suggest how a workaround for this problem? I am unable to post the full .xlsm file.

Also, I am aware to avoid using .Select and .Activate but for the code above, I couldn't get it to work at all when I wasn't using this.

Thanks in advance,
Jack
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Ignore this post, I'm going to write a procedure to re-generate the chart on each file output creation, instead of copy and paste.
 
Upvote 0
I'd have tried specifying a destination to paste to:

Code:
oChart1.Copy
            'wkb.Activate
            With Sheets(PvtTable4)
                .Paste .Range("B3")
            End With
for instance.
 
Upvote 0
Thank you Rory, much appreciated. Will try your suggested change,
== Jack
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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