+ Reply to Thread
Results 1 to 13 of 13

Running macro twice in quick succession

  1. #1
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Running macro twice in quick succession

    Hi

    My workbook (attached) has a macro "NewParticipator()" the execution of which takes about half a second from invocation to conclusion on my Excel 2003 machine (running under Citrix, Windows Server 2003).. The macro is invoked by clicking on the button labelled "P" in the commandbar attached to the workbook.

    If I double-click in very rapid succession on the "P" button, the macro executes twice (as desired) and executes correctly.

    If I click on the the "P" button, wait until the macro completes, and then click on it again, then again the macro executes twice, with correct results, as desired.

    If I click on the "P" button twice in FAIRLY rapid succession, the second click being just before the conclusion of the first execution, maybe about a quarter of a second, or about half way through the first execution, then the macro runs twice (as desired) but the result contains an error.

    The error is in the formula contained in Column K of the worksheet "Participators". In the first row (ie the row that is created by running the macro the first time), the formula in the relevant cell in column K appears as
    ='Provision (0)'!$D$2
    and retunrs a value
    "Provision (0)"
    This is undesired. "Provision (0)" should never appear in the formula or result returned in any cell in column K of the worksheet "Participators", and the correct formulae and values are as returned when running the macro twice either with a delay between them or with a rapid double-click.

    Can anyone tell me please
    (1) what is causing this undesirable behaviour, and
    (2) what should I do to combat it (other than avoiding running the macro with a delay of a quarter of a second between executions)?

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Running macro twice in quick succession

    You can disable other macro events from starting until it has completed.
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Running macro twice in quick succession

    Thanks for the suggestion, but I regret to say that it had no effect on the problem stated in the OP, as demonstrated in the attachment to this response.

    In this attachment I have disabled EnableEvents in the private sub PrepareStart(), which sub is called as the first instruction of the offending macro. And yet the symptoms described in the OP persist.

    Any other suggestions, folks?

    Thanks.

    [EDIT]
    PS. I have just now tried it out on my home machine, running Excel 2007 under Vista Home Premium, and the same (or I should say ALMOST the same) problem arises. The only difference is that it is column J of the worksheet "Participators" which offends, in returning values "Summary (0)" in error, while in this case column K seems to come out fine each time. And yes, this is using the version with EnableEvents set to False.
    Attached Files Attached Files
    Last edited by 1eyedjack; 06-12-2010 at 06:43 AM.

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

    Re: Running macro twice in quick succession

    maybe...
    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

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

    Re: Running macro twice in quick succession

    maybe...
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Running macro twice in quick succession

    Thank you very much for taking the trouble to look at my problem.

    I am not sure where to enter the code that you suggest. It looks very much like the sort of code that I would attach to a commandbutton such as might be entered onto a worksheet from the Forms toolbar. This macro is being invoked by clicking on a button in a commandbar (at one time I think it used to be called a toolbar) attached to the workbook. At the moment I cannot even see how to identify the name of the particular button on the command bar. I rightclick on the bar, then choose Customize. Then I right-click on the offending button, expecting to see "Properties ..." in the resulting popup menu, but it is not there.

    That said, I think I may have found a kludgy workaround. In all of my previous attempts I set calculation to manual for the duration of the macros, then reset to automatic on conclusion. If the workbook needed to recalculate midway through its execution then I would insert an instruction to calculate at that point in the code. By and large that seemed to work OK, and it certainly cut down on the execution time of macros. However in this case I have just tried the effect of setting calculation to automatic throughout, and I have not yet been able to reproduce the original problem having made that change. The main trouble with this solution is that the execution time of the macro goes up by a factor of 4 or more. That is a lesser evil, if I have to choose between those two options. I would rather a third way, though.

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

    Re: Running macro twice in quick succession

    you could create you own commanbar which would be easier to control the "FagPack" add in

  8. #8
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Running macro twice in quick succession

    I'm sorry, I do not understand this. The macro is run from a button on a commandbar which I *did* create, and then attached to the workbook.

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

    Re: Running macro twice in quick succession

    Hi 1eyedjack,

    No, I meant to make it from scratch instead of making it by Commands>Marcros>custom button eg.. from J Walkenback
    Please Login or Register  to view this content.
    this way you can "enable= false" the button on start the code the "enable= true" at the end.

  10. #10
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Running macro twice in quick succession

    Ah, now I understand what you mean, I think.
    At least, until I try and fail I think I understand. May come back here if I run into problems.
    Thanks for the help

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

    Re: Running macro twice in quick succession

    Hi 1eyedjack
    Looking at your code move all the "PrepareStart" code to the first lines in "NewParticipator". I have had "EnableEvents" cause problems if its iniated that way.

  12. #12
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Running macro twice in quick succession

    I have tried putting the PrepareStart() code into the main body of the NewParticipator() routine, but it made no noticeable difference in relation to the problem.

    Thanks for the tip, though, I shall note that for the future.

  13. #13
    Registered User
    Join Date
    06-11-2010
    Location
    grinnell, iowa
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: Running macro twice in quick succession

    If it's just an error that is screwing up the second run of your code, you could append this to your code, which will find any errors in columns(K) and replace them with the text "ERROR," which may or may not be desirable:
    Please Login or Register  to view this content.
    Also, maybe create a master piece of code that says:
    Please Login or Register  to view this content.
    With the appropriate code below that, and just run the mastercoder sub once?

    **Edit: I realize you might have meant that the cell in column K isn't literally an error. Try this code instead in that case, though I would still use the mastercode method:
    Please Login or Register  to view this content.
    Unfortunately I can't download your file currently, but see if that code above works (you may have to change With Sheet1 to whatever Sheet you're using).
    Last edited by Jbm444; 06-16-2010 at 11:48 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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