+ Reply to Thread
Results 1 to 12 of 12

Prevent macro from Running Twice in one day?

  1. #1
    Forum Contributor
    Join Date
    11-27-2009
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2013 & 2016
    Posts
    409

    Prevent macro from Running Twice in one day?

    Hi All,
    I have a macro that will run from a form button.
    The macro performs various tasks within my database, this macro is run daily with a new data file, I compare the current data file to the previous day's and report on the changes between the two files.
    What I want to do is if the macro has been run for example, today already, I don't want to be able to run it again till tomorrow.
    Here is the code I have on the import button.

    Please Login or Register  to view this content.
    Any assistance is greatly appreciated!
    ==========
    Bigroo1958
    Austin, Texas
    ==========

  2. #2
    Registered User
    Join Date
    07-31-2014
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    3

    Re: Prevent macro from Running Twice in one day?

    Make your code to save the last run date to somewhere in a worksheet, and save the worksheet. Also at the beginning in your macro to compare between the current date and saved date before any other codes can be executed.

  3. #3
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Prevent macro from Running Twice in one day?

    I would store the day the code runs somewhere. A cell, the sheet properties(like the author field) and compare that date with today's date.
    David
    (*) Reputation points appreciated.

  4. #4
    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,902

    Re: Prevent macro from Running Twice in one day?

    Add a hidden Control or Configuration sheet. On that sheet, use a cell, say A1 to hold the last date that the routine was run.

    When the routine is run, the first thing it should do is check if the cell contains the current date. If it does, it must exit. If it doesn't, it can store the current date in the cell and continue execution.

    Two lines of code.


    Regards, TMS
    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


  5. #5
    Forum Contributor
    Join Date
    11-27-2009
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2013 & 2016
    Posts
    409

    Re: Prevent macro from Running Twice in one day?

    Thanks Guys for the replies!

    So, I should setup a table in my database to save the last date run and compare this date with the current date?
    I never thought of that, great idea.
    Do any of you have example code to do this in Access?

  6. #6
    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,902

    Re: Prevent macro from Running Twice in one day?

    Sorry, I think we all missed that it was an Access query.

    Whatever, a simple table with one record/entry would serve the purpose. No code, unfortunately.

    Basically, get the record, check it, update it if the date is different, exit or carry on.

  7. #7
    Forum Contributor
    Join Date
    11-27-2009
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2013 & 2016
    Posts
    409

    Re: Prevent macro from Running Twice in one day?

    Thanks for the reply TMS!

    I worked on this some and here is what I have come up with!
    I added a new table to hold the date the macro has last run. If the macro has not been run today, I have a doCmd RunSql update statement to enter in current date into the table. The problem I am having is the comparison from date() to date in table, not quite working right. Can someone point me in the right direction or am I over thinking this???
    I don't think I have the if statements correct!

    Please Login or Register  to view this content.
    Last edited by bigroo1958; 01-01-2015 at 11:44 AM.

  8. #8
    Forum Contributor
    Join Date
    11-27-2009
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2013 & 2016
    Posts
    409

    Re: Prevent macro from Running Twice in one day?

    After further testing, I think this works for me!!

    Please Login or Register  to view this content.

  9. #9
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Cool Re: Prevent macro from Running Twice in one day?

    Here is the code you can try:-
    I have used Registry settings to get the required job done.
    Use the following sub-routine to initialize your main function. (Untested though)
    Please Login or Register  to view this content.
    Last edited by Vikas_Gautam; 01-02-2015 at 01:16 AM.
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  10. #10
    Forum Contributor
    Join Date
    11-27-2009
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2013 & 2016
    Posts
    409

    Re: Prevent macro from Running Twice in one day?

    Thanks so much Vikas for the reply,

    This is not in MS Excel but MS Access VBA, will your code example work in Access?

  11. #11
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Cool Re: Prevent macro from Running Twice in one day?

    Yes Bigroo, The above code does work with Access ( Since I have made some changes now ).
    The reason is that I haven't use any excel object here.
    Check the attached Access file ( It contains the code may be, as its my first time with Access Vba Module. Ha Ha )
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    11-27-2009
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2013 & 2016
    Posts
    409

    Re: Prevent macro from Running Twice in one day?

    Thanks Vikas for the reply and database example!!

    Also, thanks to everyone else who stopped by and replied, I appreciate the input!!
    Thanks

+ 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. How to prevent error when running a macro
    By aharvestofhealth in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-18-2010, 04:14 PM
  2. Prevent functions from running while macro is running
    By palpha32 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-24-2009, 08:42 PM
  3. Prevent private sub macro from running
    By jman0707 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-15-2008, 01:35 AM
  4. Prevent a user from running a macro
    By vbace2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-27-2006, 11:28 PM
  5. Prevent A Macro From Running If SpreadSheet is Filtered
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 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