+ Reply to Thread
Results 1 to 15 of 15

If a cell already has a value, do not run the macro?

  1. #1
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    If a cell already has a value, do not run the macro?

    I have a macro which populates cell "J6" with a new report number every time the spreadsheet is opened, and another macro which saves the document to a new spreadsheet using the value in cell "J6"

    Once the spreadsheet has been saved using the value in cell J6, if i open the newly created excel document named after cell J6, it generates another number and overwrites the previous value in cell J6.

    This is my code:
    Please Login or Register  to view this content.
    Is it possible to modify this, so that if there is already a value in cell J6, it will not run?


    Thanks
    Last edited by jond291; 09-11-2014 at 07:54 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,607

    Re: If a cell already has a value, do not run the macro?

    Try

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Thanks for the reply but it doesn't work, when I open the newly created spreadsheet it still replaces J6 with a new number.

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,607

    Re: If a cell already has a value, do not run the macro?

    Can you upload example workbook?
    Make sure to remove confidential informations.

  5. #5
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Ok, I've uploaded it.

    Just so you know the way ti works is:
    Open the spreadsheet and it runs a macro to find the most recent file in a directory.
    Another macro which removes the .xlsx extension of the file
    Another macro which then adds 1 to the file, so it's the next report number
    And finally another macro which copies the new value into cell J6

    The save button then saves the sheet based on the value in cell J6


    P.S. The code may be a bit messy, there is a lot of different things happening, and I've tried many things to get it to work, I'm just not sure what I can and can't delete anymore.
    Last edited by jond291; 09-11-2014 at 04:41 AM.

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,607

    Re: If a cell already has a value, do not run the macro?

    Maybe you want to put it in Module4:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Unfortunately that doesn't work either, would it be possible to achieve the same results in a different way, maybe by telling it to save as a read-only?

  8. #8
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,607

    Re: If a cell already has a value, do not run the macro?

    I really don't know what you try to achieve and when you just answer "that doesn't work" doesn't help me to understand further problem

  9. #9
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Ok sorry, i'll try to be more detailed, this is what happens when I make the change your suggested:

    I click the save button, which saves the file as a new document named after the value in cell J6.
    When I open the newly created document, I want it so that the value in cell J6 is still the same as when it was saved.
    However due to the macro which updates the cell every time the document is opened, it keeps changing to a different number.

    I need the macro which updates J6 (In the master spreadsheet) to not run once the file has been saved.

    I hope this is clear enough as I really appreciate your help.

  10. #10
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,607

    Re: If a cell already has a value, do not run the macro?

    Then you want just delete macro on Workbook open?
    So it won't change when it's opened?

  11. #11
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Yes but only on the newly created spreadsheet, which is created using the save button, I still need the macro to run on the main spreadsheet. (The one I attached)

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: If a cell already has a value, do not run the macro?

    Do you save the main workbook with J6 populated?

    I'd suggest you use a template file rather than a workbook, personally.
    Remember what the dormouse said
    Feed your head

  13. #13
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Quote Originally Posted by romperstomper View Post
    Do you save the main workbook with J6 populated?

    I'd suggest you use a template file rather than a workbook, personally.
    The main workbook is saved without J6 populated, but when you open it, J6 is automatically populated. I then click save and it saves to a new file called whatever is in J6. However it is once I open this new workbook that I do NOT want the macro to run, as it currently overwrites what ever was in J6.

  14. #14
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: If a cell already has a value, do not run the macro?

    Then, as zbor already suggested I think, you simply change the Workbook_Open code in the ThisWorkbook module to something like this:
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    05-31-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: If a cell already has a value, do not run the macro?

    Yes!!

    That's It!

    Not sure why zbor's didn't work, the 2 answers are slightly different so must be something different in there somewhere.

    But it's working

    Big thanks to both of you, really appreciate it.

+ 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: 2
    Last Post: 08-13-2014, 06:39 PM
  2. [SOLVED] Macro Delete Contents in 23rd corresponding cell if the cell in range is a non-blank cell
    By murtaza.khan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-04-2014, 01:11 AM
  3. macro to run a macro for evrey cell in column depending upon valuein cell
    By swaptul in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-09-2014, 06:55 PM
  4. Replies: 1
    Last Post: 11-06-2013, 02:56 AM
  5. Cell Rounding Macro - significant digits is variable from cell to cell
    By schmidt62 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-04-2012, 05:06 PM

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