+ Reply to Thread
Results 1 to 9 of 9

Code to keep a macro from activating more than once?

  1. #1
    Registered User
    Join Date
    06-08-2009
    Location
    Annapolis, Maryland
    MS-Off Ver
    Excel 2007
    Posts
    85

    Code to keep a macro from activating more than once?

    Hello all.

    I have a macro I want the user to click once and no more. Is there something I can add to the code that will do such a thing?

    Edited to add: The macro saves the file to a specific location. The problem is when it's run more than once, it causes an error message which freaks the user out. lol

    Thanks.
    Last edited by debbiesh; 05-24-2012 at 02:52 PM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code to keep a macro from activating more than once?

    debbiesh,

    When I want to do something like that, I reserve an out of the way cell (usually the cell in the first row the last column of the file) to store either 0 or 1. If it is a 0, then let the macro run, and at the end of the macro, change the cell to a 1. So that when the macro goes to run again, it checks the cell which is now a 1 and so the macro exits. It looks like this:
    Please Login or Register  to view this content.


    You can change that cell back to a 0 using a different macro, or the workbook_open event, or change it manually if you want the macro to run again.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    06-08-2009
    Location
    Annapolis, Maryland
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Code to keep a macro from activating more than once?

    Thank you for your reply. I'll give it a shot asap and let you know what happens.

  4. #4
    Registered User
    Join Date
    06-08-2009
    Location
    Annapolis, Maryland
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Code to keep a macro from activating more than once?

    Keep getting 404 error. What am I doing wrong?

    Please Login or Register  to view this content.

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code to keep a macro from activating more than once?

    debbiesh,

    This doesn't exist: Cells(AZ2, Columns.Count)
    If you want to use AZ2, then instead use: Range("AZ2")

  6. #6
    Registered User
    Join Date
    06-08-2009
    Location
    Annapolis, Maryland
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Code to keep a macro from activating more than once?

    Ahh, right! Thanks. So it saves correctly when I click the button to activate the macro, and AZ2 changes to 1. Perfect.
    Then when I click the button a second time, I get a 404 error message. What would cause the error at this point?

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code to keep a macro from activating more than once?

    You need to put all of the code in the IF statement. You currently have the activeworkbook.save and the msgbox outside of the if statement

  8. #8
    Registered User
    Join Date
    06-08-2009
    Location
    Annapolis, Maryland
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Code to keep a macro from activating more than once?

    Hey, I'm learning here. LOL It works perfectly now! Thanks so much, tigeravatar.

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code to keep a macro from activating more than once?

    You're very welcome

+ 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