+ Reply to Thread
Results 1 to 8 of 8

Run Time Error 1004 when copying data

  1. #1
    Registered User
    Join Date
    06-08-2012
    Location
    Durban, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    7

    Red face Run Time Error 1004 when copying data

    Hi All

    I have written the following macro in Excel 2010 to copy data and then transpose the copy using paste special. It runs fine the first time but as soon as you delete the result and run it again it gives the Run time Error 1004 - Application Defines or object Defined error.

    If you select end on the run time error window and run it again it works fine. The error occurs at the line in red.

    Sub Sample()
    i = Application.WorksheetFunction.CountA(Sheet10.Range("A:A"))
    j = Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))


    Sheet1.Range("H1", Sheet1.Cells(j, 8)).Copy
    Sheet10.Range("A1").PasteSpecial xlPasteValues
    Sheet10.Range("A:A").RemoveDuplicates (1)

    Sheet10.Activate
    Sheet10.Range("A1").Select
    Sheet10.Range(Sheet10.Cells(2, 1), Sheet10.Cells(i, 1)).Copy

    Sheet11.Activate
    Sheet11.Range("A1").Select
    Sheet11.Range("A1").PasteSpecial xlPasteAll, , , True



    End Sub

    Any assistance as to the cause of this error would be appreciated.

    Regards

    Sharksfan88
    Last edited by Sharksfan88; 06-08-2012 at 05:32 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Run Time Error 1004 when copying data

    Maybe a sample workbook ...
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Run Time Error 1004 when copying data

    Though, thinking about it, if you clear the results, won't j = 0 next time you run it?

  4. #4
    Registered User
    Join Date
    06-08-2012
    Location
    Durban, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run Time Error 1004 when copying data

    Hi TM

    Thanks for reply, the variable j is from the source sheet1 so does not reset to 0. I am only deleting the result on sheet 10 and 11.

    Sharksfan88

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Run Time Error 1004 when copying data

    OK, so where does i come from?

    By the way, to comply with forum rules, you should add Code Tags to your code excerpt in post #1.

    Regards, TMS

  6. #6
    Registered User
    Join Date
    06-08-2012
    Location
    Durban, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run Time Error 1004 when copying data

    When you refer to code tags you are referring to Comments right, Sorry for being a noob , if so i will insert tags. Thanks alot you have sorted it out for me. The variable needs to be declared before the second part of the macro as the second time it runs it will reset i to 0, now i really feel stupid.

    Thanks again

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Run Time Error 1004 when copying data

    Code Tags are [Code ]Please Login or Register to view this content.[/Code]

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Run Time Error 1004 when copying data

    Thanks for the rep. You need to look at the forum rules for information about Code Tags (Rule 3, to be specific)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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