+ Reply to Thread
Results 1 to 7 of 7

Prevent a macro being run twice in one day!

  1. #1
    Registered User
    Join Date
    06-25-2007
    Posts
    35

    Prevent a macro being run twice in one day!

    Hi All

    I have a workbook which contains macros. These macros have to be run in sequence (1-6) at the end of each week. If the person running them re-runs one of them twice it corrupts data in another workbook.
    How can I prevent this from happening?
    Obviously if an error occurs (e.g runtime error) it would then need to be re-run so any code would have to allow for that!

    Thanks!!!!!!!!!!!!

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You will need to set an entry that the macro can accress to see when it was last run.

    This setting can be an entry in a cell. There are some cell Properties that are only aviable from VBA code and can be used for this purpose as long as the workbook is saved to retain the value.

    You could also use a registry entry. A registry entry would be valid only on the pc that ran the macro.

    You could use a text file that could be saved on a local or net drive to hold the last run date.

    What ever method you use I suggest that the entry be made at the end of the macro run to allow the macro to be rerun if it crashes on the previous run or if you update the entries when the macro 1st runs you will need to add code to warn the user that the macro had already been started and may have already been run this week whislt allowing the user to cancel or allow the macro to run.

    If any of the above suggestions can be implemented I can give you some guidence on some VBA code
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    06-25-2007
    Posts
    35
    Hi Mudraker

    Thanks for your reply.
    Thie cell entry would be the best option for me as I need to send the workbook out to many sites.
    Could you send me some example code please?

    Thanks!

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Here is 2 examples

    Neither example takes into account of multiple sheets & will test via the activesheet.
    Sheet references can be added if required.
    The workbook must be saved after the date is added to ensure it tests correctly


    This example uses A1
    The entry is viewable & changable by any user
    A1 can not be used for any other purpose
    You can change from A1 to anyother cell that suits


    Please Login or Register  to view this content.

    In this example the macro also uses A2
    The entry is only visible and can be changed only by macro code
    A2 can still be used in a normal maner like any other cell.
    Deleting A2 will cause the macro to beeive the macro has not run this week
    Please Login or Register  to view this content.
    Another option is to use add another worksheet that can be hidden in so that it can only be accessed by macro code and use 1 of the above examples on this additional sheet

  5. #5
    Registered User
    Join Date
    06-25-2007
    Posts
    35

    Thumbs up

    Hi Mudraker

    This is exactly what I need!!

    As always you guys are awesome!!

    Thanks for your help.

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Glad to have been able to help solve your problem

    PS which method did you use?

  7. #7
    Registered User
    Join Date
    06-25-2007
    Posts
    35

    Thumbs up

    I used the first example (A1). The cells I used are locked and the worksheet password protected. At the begining of the code I unprotect the worksheet and then protect again at the end. This obviously prevents users changing the cells as they don't know the password.

    Thanks again!!

+ 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