+ Reply to Thread
Results 1 to 9 of 9

Error 1004: Pastespecial method of range class failed, macro works sometimes!

  1. #1
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Hello,

    I have a macro that works by pasting formulas into cells and then pasting over them with the values produced by the formulas. It works fine for 500 iterations then crashes at this line:

    Please Login or Register  to view this content.
    The macro has worked once, and if I reduce the total number of iterations required from 65 on i and j then it has worked, so I know that this isn't a problem with one of the calculations resulting in an error so not being able to paste properly.

    I ahve seen similar help topics and tried to do what has been advised (i.e. remove 'selection' where I can and explicitly define the range instead) but so far no joy...the macro has actually worked in its entirety once, which just makes it even more infuriating!

    If anyone can think of anything that may be going wrong I would be really grateful for some help. Thankyou in advance.

    Here is the code in its entirety:

    Please Login or Register  to view this content.
    Last edited by sam0287; 12-19-2009 at 09:15 PM.

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Hi Sam,

    welcome to the forum. I don't do VBA, but my first thought was:

    any merged cells involved?

  3. #3
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Hey mate thanks for the quick response.

    Unfortunately not no, although now that you remind me I guess it might be significant that it pretty much always messes up at the same cell.

    In the 'Probabilities' sheet, row = 513, column number 39. columns 1-38 contain values so the paste values worked but from 39-137 the formulas remain so looks like its getting stuck on this cell. As I said before though the calculation itself does not poduce an error so it should be able to paste the value in, but it doesn'e hence I'm here

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Hi Sam0287
    This bit wont work
    Please Login or Register  to view this content.
    what are you trying to do?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    I think this duplicates your code without the Selects:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Hey mate,

    Ye sorry its a little confusing, 'Exp(-Generalised Cost)' is actually the name of one of the worksheets, so that bit is just selecting the sheet by its name...

    Generalised cost is a value (lambda) divided by the frequency, which is generated in the previous sheet... Lambda is defined in a cell in the workbook (as 1).

    The model is a multinomial logit model, so its using the exponentials to calculate probabilities from a specified function involving exp(-generalised cost) for different costs. I've split the process up into stages in an attempt to make it easier to follow, since some people that will need to use it don't know vba, so the outputs from each stage are being stored in the workbook so that they can be checked...

    I get the maths, but this programming malarchy is really throwing me off!

  7. #7
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Oh thanks a lot shg, I'll give it a try and report back.

    Are selects a no no for this stuff then?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Selecting doesn't disturb the logic, it just makes code run slower, and is rarely necessary.

    But you should always qualify your references as a matter of good practice.

  9. #9
    Registered User
    Join Date
    12-19-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Error 1004: Pastespecial method of range class failed, macro works sometimes!

    Ok, solved the problem!

    Turns out the worksheet I was trying to paste into where the code was screwing up was corrupted! After the macro failed I tried to paste in the formulas and then paste over them with values in the same row and it simply wouldn't let me do it...

    So I deleted the sheet and recreated it to be identical to how it was originally and the code worked fine!

    How infuriating, but nonetheless problem solved. Thanks to everyone for suggestions etc.

    P.s. thanks shg, without the selections the code literally runs in half the time!
    Will definitely avoid selections in future when possible...

+ 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