+ Reply to Thread
Results 1 to 7 of 7

Runtime Error '1004' when Copying Charts

Hybrid View

  1. #1
    Registered User
    Join Date
    03-14-2014
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2010
    Posts
    11

    Runtime Error '1004' when Copying Charts

    Guys,

    I have a code that will automatically make a powerpoint presentation from several charts in my excel file. I get a runtime error when running the code on a few of the lines where the charts are copied. I hit debug, go to the actual chart in excel click on it, then go back to code and run and it works till the next time the Copy code comes up. So now I have to hit debug like 5 times then physically click the Chart the code errors out on, then continue running the code. If I immediately run the code again after I have went through and clicked the errored out charts, it works perfectly. IE once I have clicked them once I can run the code again and again without the error. Once I close the workbook and reopen does the Runtime error reoccur.


    I figure it is a syntax error but not sure why clicking on the chart makes it work???
    Any help is greatly appreciated.

    Here is the specific code it errors out on
    ActiveWorkbook.Worksheets("MTN_Charts").ChartObjects("MTN").Copy
    Here is the entire code
    Sub ChartToPresentation()
        'Automatically create powerpoint presentation using Template and pasting in trend graphs
        'Declare variable and type
        Dim PPApp  As PowerPoint.Application
        Dim PPPres As PowerPoint.Presentation
        Dim PPSlide As PowerPoint.Slide
        Dim oPPshape As Object
        Dim Sld As PowerPoint.Slide
        Dim cht As PowerPoint.Chart
        Dim kpitype, SaveDatestrg As String
        Dim SaveDate As Date
        Dim fileNameString As String
        
        'Define values
        fileNameString = "X:\Gordonsville\Production Meetings\Daily GM Visuals\GMVisuals " & Format$(Date, "mm-dd-yyyy")
        kpitype = Range("B5").Value
    
            ' Open PowerPoint
            Set PPApp = CreateObject("Powerpoint.Application")
            ' Open specific template
            Set PPPres = PPApp.Presentations.Open("X:\Gordonsville\Production Meetings\GmVisualsTemplate.potx")
            
            'Set subtitle dependent on Kpi selected
            PPPres.Slides(1).Shapes(2).TextFrame.TextRange.Text = kpitype
        
        
        ' Begin copy and paste of Charts to specific slides
            ActiveWorkbook.Worksheets("MTN_Charts").ChartObjects("MTN").Copy
            With PPPres.Slides(2).Shapes.Paste
                ' Resize and Align pasted chart
                .Width = 697.122
                .Height = 419.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
                '.Line.ForeColor.RGB = RGB(192, 80, 77)
                            
            End With
            
            ActiveWorkbook.Worksheets("MTN_Charts").ChartObjects("Cumberland").Copy
            With PPPres.Slides(3).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
               '.Line.ForeColor.RGB = RGB(192, 80, 77)
               End With
               
           
            ActiveWorkbook.Worksheets("MTN_Charts").ChartObjects("Gorwood").Copy
            With PPPres.Slides(4).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
            End With
                
                'Skip elmwood if ore hoisted
                     If kpitype = "Ore Hoisted" Then
                        With PPPres.Slides(5)
                            Set oPPshape = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=100, Top:=50, Width:=400, Height:=100)
                            oPPshape.TextFrame.TextRange.Text = "Hoisting not available at Elmwood!" & vbNewLine & "All ore produced at elmwood is transported and hoisted in Gordonsville"
                        End With
                     GoTo Skip
                     Else
                     
                ActiveWorkbook.Worksheets("MTN_Charts").ChartObjects("Elmwood").Copy
            With PPPres.Slides(5).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
            End With
                End If
    Skip:
    
            ActiveWorkbook.Worksheets("ETN_Charts").ChartObjects("ETN").Copy
            With PPPres.Slides(6).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
            End With
            
            ActiveWorkbook.Worksheets("ETN_Charts").ChartObjects("Coy").Copy
            With PPPres.Slides(7).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
            End With
            
            ActiveWorkbook.Worksheets("ETN_Charts").ChartObjects("Immel").Copy
            With PPPres.Slides(8).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
            End With
            
            ActiveWorkbook.Worksheets("ETN_Charts").ChartObjects("Young").Copy
            With PPPres.Slides(9).Shapes.Paste
                ' Align pasted chart
                .Width = 697.122
                .Height = 428.42
                .Align msoAlignCenters, True
                .Align msoAlignMiddles, True
            End With
            'Savefile
            PPPres.SaveAs fileNameString, 1
            
            ' Clean up
            Set PPSlide = Nothing
            Set PPPres = Nothing
            Set PPApp = Nothing
            
    End Sub

  2. #2
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Runtime Error '1004' when Copying Charts

    Hi,

    Are there multiple sheets in your workbooks, If Yes, check whether at the time of copy, sheets is activated or not.

    If not, use the following code:

    ActiveWorkbook.Worksheets("MTN_Charts").Activate

    Note: Paste this line of code after this line "Begin copy and paste of Charts to specific slides"

    Regards,
    Paresh J
    Click on "* Add Reputation" as a way to say thanks

  3. #3
    Registered User
    Join Date
    03-14-2014
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Runtime Error '1004' when Copying Charts

    Yes probably 10 sheets in the workbook all with specific names.

    I have tried adding the code you supplied to activate each sheet where the chart exists but still get the runtime error.


    Is there a way to activate the named ChartObject ?

  4. #4
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Runtime Error '1004' when Copying Charts

    Hi,

    Yes, You can activate the chart objects as well. With little modification in the above code.

    ActiveWorkbook.Worksheets("MTN_Charts").ChartObjects("MTN").Activate

    Regards,
    Paresh J

  5. #5
    Registered User
    Join Date
    03-14-2014
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Runtime Error '1004' when Copying Charts

    Yes,
    I added a chart activate line before each and it appears to be working.


    Thank you for the assistance.

  6. #6
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Runtime Error '1004' when Copying Charts

    Hi,

    If you think you got your solution then please mark this thread as SOLVED and click * Add Reputation as way to say thanks....


    Regards,
    Paresh J

  7. #7
    Registered User
    Join Date
    11-02-2020
    Location
    Belgium
    MS-Off Ver
    Office 13
    Posts
    1

    Re: Runtime Error '1004' when Copying Charts

    Had the same problem copying charts form one worksheet to another
    Seems to work fine now
    THX!

+ 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. Runtime error 1004 when copying & pasting data to another sheet
    By luneke1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2014, 06:04 PM
  2. Runtime 1004 Error when copying
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-03-2013, 09:51 AM
  3. Runtime Error 1004 while copying dinamic ranges
    By mallok in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-29-2013, 09:37 AM
  4. [SOLVED] Runtime Error 1004 when Copying worksheets from multiple workbooks into current workbook
    By dharris42 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-27-2013, 11:56 AM
  5. Runtime error 1004 when copying and pasting a filtered collumn excel 2010
    By Shedacq in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2012, 06:35 AM

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