+ Reply to Thread
Results 1 to 17 of 17

Run macro only once based on cell value

  1. #1
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Run macro only once based on cell value

    Hi there, I currently have a macro run if cell E2 has the text "In"

    If cell E2 has any other word / value other than "In" - the macro shouldn't run.

    How can I get the macro to only run once (ie the first time the cell E2 changes to "In") and then stop?

    Currently the macro just loops and I can find no way to stop this.

    Appreciate any help on this once

  2. #2
    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,613

    Re: Run macro only once based on cell value

    Define a global public Boolean variable. The default will be FALSE. When your macro runs, test for TRUE and, if it is, exit. If it isn't, set it to TRUE and continue with the macro.


    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


  3. #3
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Run macro only once based on cell value

    Use another cell to hold a flag to say the macro has been ran and in your macro code check that flag, if false run the code and set the flag to true

  4. #4
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Run macro only once based on cell value

    Quote Originally Posted by TMS View Post
    Define a global public Boolean variable. The default will be FALSE. When your macro runs, test for TRUE and, if it is, exit. If it isn't, set it to TRUE and continue with the macro.


    Regards, TMS
    Sorry, lost me on that one

  5. #5
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Run macro only once based on cell value

    Trevors is the better option as I missed the bit around looping however if you want to have this facility across days/ openings of the workbook then you'll need to save the value in the workbook.

  6. #6
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Run macro only once based on cell value

    Quote Originally Posted by pjwhitfield View Post
    Use another cell to hold a flag to say the macro has been ran and in your macro code check that flag, if false run the code and set the flag to true
    I tried that by marking an "X" in cell F2 - but the looping still occured

  7. #7
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Run macro only once based on cell value

    This is my current code

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Run macro only once based on cell value

    Quote Originally Posted by cmb80 View Post
    Sorry, lost me on that one
    In a module

    add the following line at the top

    Please Login or Register  to view this content.
    in your code
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    10-16-2014
    Location
    Center Line, Michigan, United States
    MS-Off Ver
    MS Office 2013
    Posts
    139

    Re: Run macro only once based on cell value

    Here's a code that only runs a) if E2 is the cell changed, and b) only if the value is changed to "In"

    Please Login or Register  to view this content.
    Please click the star (add rep) if I helped!

  10. #10
    Forum Contributor
    Join Date
    10-16-2014
    Location
    Center Line, Michigan, United States
    MS-Off Ver
    MS Office 2013
    Posts
    139

    Re: Run macro only once based on cell value

    Sorry, duplicate post

    Note: This macro will run EVERY time the cell is changed to "In" If you only want it once ever (per use of the file), then use this:

    Please Login or Register  to view this content.
    Last edited by Loganeb; 10-26-2014 at 03:07 PM.

  11. #11
    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,613

    Re: Run macro only once based on cell value

    Of course, if E2 has the text "In" in it, change it to something else ... like "Done"

    It needs to be clarified if this is once per session or once only (for ever).

    It'd help if you shared your code.

    Regards, TMS

  12. #12
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Run macro only once based on cell value

    The worksheet is a constant feed from the internet - Cell E2 will either say "In" or "Not".

    I would like the macro to run on the first occasion the cell E2 changes to "In" - once it changes to "In" it wont change back to "Not" - but it will keep refreshing every 5 seconds, and hence causing my macro to keep being called

  13. #13
    Forum Contributor
    Join Date
    10-16-2014
    Location
    Center Line, Michigan, United States
    MS-Off Ver
    MS Office 2013
    Posts
    139

    Re: Run macro only once based on cell value

    cmb80,

    Do you want the macro to work once per each time you open the file? So, if you were to open it, and change the value of E2 to "In", it would run once, then not run again for the rest of the time you're working with the file in that sitting? My code above will accomplish that. Or do you want it to run once, then basically delete itself from your workbook?

    Place that code into the sheet code (not a module) and it should work.
    Last edited by Loganeb; 10-26-2014 at 03:35 PM. Reason: Extra directions

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Run macro only once based on cell value

    The first time you run it have it post the current date in another cell. Then, subsequently, you can test that cell for the days date and, if the current date is greater than the posted date let the macro run.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  15. #15
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Run macro only once based on cell value

    thanks for your help guys, I think date stamping is possibly the best solution

  16. #16
    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,613

    Re: Run macro only once based on cell value

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  17. #17
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Run macro only once based on cell value

    You're welcome!

+ 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. Replies: 3
    Last Post: 08-16-2014, 09:55 AM
  2. Macro to change colour of cell based on finding a specific word in the cell
    By Shelby761 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2014, 09:46 AM
  3. Create Auto-Calculating Cell and Assign Cell Values Based on a Macro
    By Titanium Fox in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-26-2013, 12:00 AM
  4. Macro that will clear contents of cell based on format of text in adjacent cell
    By judasdac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2012, 01:56 AM
  5. Macro to auto populate the destination cell based on the values of the previous cell
    By Avinash Kumar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2009, 02:06 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