+ Reply to Thread
Results 1 to 2 of 2

Excel Macro requires me to restart my computer to copy and paste graphs to a new sheet

  1. #1
    Registered User
    Join Date
    10-20-2022
    Location
    Texas
    MS-Off Ver
    Excel 2016
    Posts
    1

    Excel Macro requires me to restart my computer to copy and paste graphs to a new sheet

    Yesterday I recorded a macro that copies data and three graphs from one worksheet and pastes them onto a new sheet. The first time I ran the macro, it copied and pasted the data and graphs perfectly. The second time (and every time after that), it pasted the data correctly, and then the code broke when it tried to copy a graph. I tried closing and re-opening Excel, and running the macro on a duplicated workbook, but I still kept getting the error: "Run-time error '-2147221040 (800401d0)': Method 'Copy' of object 'ChartArea failed." I restarted my computer and then re-ran the macro, and again, it only worked correctly the first time and then ran into the same error. And the error occurs randomly on any one of the three graphs with each run. I showed it to my co-worker, and she thinks there may be some kind of duplication going on in the background that causes the copy to fail until I restart my computer. I'm very new to macros and have no idea what other commands I could try, or what kind of troubleshooting could remedy this. I attached the code below, and would appreciate any help.

    Sub Macro12()
    '
    ' Macro12 Macro
    '

    'Generate new report
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Select
    ActiveSheet.Name = "RENAME AS TODAY'S DATE"

    'Add Flow and Belt Press Data
    ActiveWindow.SmallScroll Down:=3
    Sheets("Data").Select
    ActiveWindow.SmallScroll Down:=-12
    Range("A1:B36").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("RENAME AS TODAY'S DATE").Select
    ActiveWindow.SmallScroll Down:=-18
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste
    ActiveWindow.SmallScroll Down:=18
    Sheets("Data").Select
    ActiveWindow.SmallScroll Down:=21
    Range("A39:D53").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("RENAME AS TODAY'S DATE").Select
    Range("F41").Select
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste

    'Add Flow Graph
    ActiveWindow.SmallScroll Down:=3
    Sheets("Data").Select
    ActiveWindow.SmallScroll Down:=6
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartArea.Select
    Application.CutCopyMode = False
    ActiveChart.ChartArea.Copy <---------------------------- Code Breaks Here
    Sheets("RENAME AS TODAY'S DATE").Select
    Range("A41").Select
    ActiveSheet.Paste

    'Add BP Graph 1
    ActiveWindow.SmallScroll Down:=6
    Sheets("Data").Select
    ActiveSheet.ChartObjects("Chart 2").Activate
    Application.CutCopyMode = False
    ActiveChart.ChartArea.Copy <-----------------------------Or Here
    Sheets("RENAME AS TODAY'S DATE").Select
    Range("A56").Select
    ActiveSheet.Paste

    'Add BP Graph 2
    Sheets("Data").Select
    ActiveSheet.ChartObjects("Chart 3").Activate
    Application.CutCopyMode = False
    ActiveChart.ChartArea.Copy <-----------------------------Or Here
    Sheets("RENAME AS TODAY'S DATE").Select
    Range("D56").Select
    ActiveSheet.Paste
    Range("D49").Select
    End Sub
    Last edited by nomoreerrorsplease; 10-20-2022 at 02:06 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Excel Macro requires me to restart my computer to copy and paste graphs to a new sheet

    Hi there,

    It would be helpful if you attached a sample file (removing or anonymizing any sensitive/confidential data) showing what you have and what you need to do (see yellow banner at the top).
    I did create a file with some dummy data and loaded your code.
    It worked fine the first time. I did delete the new sheet the macro did create and run the code again. Worked fine.
    I then did rename the created sheet and run the code again. No problem. I repeated this a few times, worked each time.


    Recording macros is a great way to learn. The recorder however does sometimes record a lot of unnecessary lines.
    Here is a slightly cleaner/shorter version of your code.

    Please Login or Register  to view this content.
    If your Question is answered; please mark it SOLVED. If you are happy with a member's solution, say 'Thanks' and click the 'Star' to Add Reputation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Simple Copy paste requires running macro two times
    By chetanhm in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 02-15-2022, 10:25 AM
  2. [SOLVED] How to copy and paste 4 excel graphs into a single PowerPoint slide evenly?
    By Mirisage in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 03-07-2015, 12:47 AM
  3. Replies: 1
    Last Post: 06-20-2014, 04:48 PM
  4. macro to copy graphs from excel sheet to outlook mail
    By kmksprasad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2013, 01:24 PM
  5. Debug Cleared when restart computer
    By lalicom in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2009, 06:46 PM
  6. Replies: 1
    Last Post: 07-01-2006, 05:40 AM
  7. [SOLVED] Automatic Updates should not be able to restart your computer when
    By junebug in forum Excel General
    Replies: 0
    Last Post: 04-15-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1