+ Reply to Thread
Results 1 to 8 of 8

Error in macro when auto-pasting code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    77

    Error in macro when auto-pasting code

    Hey all,

    I'm new to VBA but have had it working well for me until the other night when a user complained that when he ran a macro, it came up with the error "Paste method of Worksheet class failed". I should note that this function has worked time and time again, so I'm lost as to why it just started. I also found tonnes of answers with a Google search but they were all relative to that persons code.

    So this is the code I am using to paste from the clipboard into another tabbed worksheet (apologies if the code is horrible) - Also the part where the debugger stops (and highlights) is ActiveSheet.Paste

    Sub PasteDemand()
    '
    ' PasteDemand Macro
    '
    
    '
        Sheets("Demand").Visible = True
        Sheets("Demand").Select
        Application.Volatile (False)
        ActiveSheet.Range("A2:X400").Clear
        Cells.Select
        Application.DisplayAlerts = False
        ActiveSheet.Paste
        Application.DisplayAlerts = True
        Columns("D:D").Select
        With Selection
        Selection.NumberFormat = "0"
        .Value = .Value
    End With
         
        Sheets("Demand").Visible = False
        Sheets("Cover").Select
        Range("C5").Select
    End Sub
    Thank you for any help!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,389

    Re: Error in macro when auto-pasting code

    Did you add this line recently?

    ActiveSheet.Range("A2:X400").Clear
    Any action other than selecting will clear the clipboard.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    01-09-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Error in macro when auto-pasting code

    Quote Originally Posted by Bernie Deitrick View Post
    Did you add this line recently?

    ActiveSheet.Range("A2:X400").Clear
    Any action other than selecting will clear the clipboard.
    Yes I added that around two weeks ago and it seemed to be working perfectly prior to just the other day. I need the data that was previously in that range to be cleared before the data in a separate workbook is pasted.. Is that possible?

    Thanks for your help.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,389

    Re: Error in macro when auto-pasting code

    Sure - the best way is to clear it first before copying, but to not change your workflow, we can check after the paste for the cells that were not pasted into....

    Sub PasteDemand2()
    '
    ' PasteDemand Macro
    '
        Dim c As Range
    
        Sheets("Demand").Visible = True
        Sheets("Demand").Select
        Application.Volatile (False)
        Cells.Select
        Application.DisplayAlerts = False
        ActiveSheet.Paste
        For Each c In ActiveSheet.Range("A2:X400")
            If Intersect(c, Selection) Is Nothing Then c.Clear
        Next c
        Application.DisplayAlerts = True
        Columns("D:D").Select
        With Selection
            Selection.NumberFormat = "0"
            .Value = .Value
        End With
         
        Sheets("Demand").Visible = False
        Sheets("Cover").Select
        Range("C5").Select
    End Sub

  5. #5
    Registered User
    Join Date
    01-09-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Error in macro when auto-pasting code

    Quote Originally Posted by Bernie Deitrick View Post
    Sure - the best way is to clear it first before copying, but to not change your workflow, we can check after the paste for the cells that were not pasted into....

    Sub PasteDemand2()
    '
    ' PasteDemand Macro
    '
        Dim c As Range
    
        Sheets("Demand").Visible = True
        Sheets("Demand").Select
        Application.Volatile (False)
        Cells.Select
        Application.DisplayAlerts = False
        ActiveSheet.Paste
        For Each c In ActiveSheet.Range("A2:X400")
            If Intersect(c, Selection) Is Nothing Then c.Clear
        Next c
        Application.DisplayAlerts = True
        Columns("D:D").Select
        With Selection
            Selection.NumberFormat = "0"
            .Value = .Value
        End With
         
        Sheets("Demand").Visible = False
        Sheets("Cover").Select
        Range("C5").Select
    End Sub
    Thank you, that worked perfectly, enjoy some +rep! So can anyone explain to me why this would have worked for the past few months, but then all of a sudden it became a problem? Or at least can anyone confirm my understanding of this - that the code I had also cleared the clipboard, thus when it went to paste there was nothing to actually paste.. right?

    Which brings me back to being confused because it worked for so long..

    Anyway thanks again!

    Oops I must have posted the thread twice by accident? My bad, sorry!

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,389

    Re: Error in macro when auto-pasting code

    The only reason that I can think of that could have led you to think the macro worked is that you have multiple versions of the macro, some changed and some not, in different versions of the workbook. The .Clear command will always clear the clipboard.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Error in macro when auto-pasting code

    I think someone here might have a similar problem.
    Last edited by Norie; 06-05-2014 at 07:14 AM.
    If posting code please use code tags, see here.

+ 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. Error in macro when auto-pasting code
    By Reapz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-04-2014, 11:26 AM
  2. Replies: 3
    Last Post: 02-09-2011, 08:40 PM
  3. Code not copying and pasting data - basic error?
    By Kayote in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-23-2008, 10:57 AM
  4. Error in Code - Pasting Values
    By Janet H in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-01-2005, 05:05 PM
  5. [SOLVED] Excel 1004 Error When Pasting Special W/ Macro
    By setoFairfax in forum Excel General
    Replies: 2
    Last Post: 04-27-2005, 10:06 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