+ Reply to Thread
Results 1 to 13 of 13

Excute Code Before Saving Workbook

  1. #1
    Forum Contributor
    Join Date
    06-24-2006
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    127

    Excute Code Before Saving Workbook

    Dear all

    Is it possible to have a piece of code that check that no cells are filled yellow before saving a workbook?

    If any of the the cells are yellow then the code will not allow the workbook to be saved?

    Thanks in advance

    Jim
    Last edited by Jim28; 06-21-2011 at 05:11 PM.

  2. #2
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Excute Code Before Saving Workbook

    Hi,

    You can try this, you will need to change the range to your range

    Please Login or Register  to view this content.
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Excute Code Before Saving Workbook

    Hi Jim,

    Yes, it is certainly possible using the Workbook_BeforeSave or BeforeClose event, however, it may be simpler to check for a logical reason why those cells are yellow, rather than the color itself as the color may be due to a manual fill color or one of multiple conditional formatting rules.

    If you know that 10 specific cells are supposed to contain data prior to saving, and you have formatting applied so that they are yellow if left blank, just check if they're blank rather than checking if they're yellow.

    Hopefully that makes sense!

  4. #4
    Forum Contributor
    Join Date
    06-24-2006
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Excute Code Before Saving Workbook

    Quote Originally Posted by Paul View Post
    Hi Jim,

    Yes, it is certainly possible using the Workbook_BeforeSave or BeforeClose event, however, it may be simpler to check for a logical reason why those cells are yellow, rather than the color itself as the color may be due to a manual fill color or one of multiple conditional formatting rules.

    If you know that 10 specific cells are supposed to contain data prior to saving, and you have formatting applied so that they are yellow if left blank, just check if they're blank rather than checking if they're yellow.

    Hopefully that makes sense!
    You read my mind Paul - yes I have set up some conditional formatting so that cells that should contain data that are empty turn yellow. I see you point about the danger of users simply manually filling the cells yellow - it it possible to avoid this?

    Re the code provided kindly by real nice guy does this go in the view code section of one of the particular sheets?

    Thanks again.

  5. #5
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Excute Code Before Saving Workbook

    With acknowledgements to rng5000 and Paul
    another idea?
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Excute Code Before Saving Workbook

    Hi that code goes in Thisworkbook under the view code of the workbook.

    .

  7. #7
    Forum Contributor
    Join Date
    06-24-2006
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Excute Code Before Saving Workbook

    Real Nice Guy - code does not work. I have placed it where you suggested - any ideas? I have put a cell in the A column in yellow.

  8. #8
    Forum Contributor
    Join Date
    06-24-2006
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Excute Code Before Saving Workbook

    Barry your code does not work either. I put a MSgbox in to test. What am I doing wrong?
    Last edited by Jim28; 06-21-2011 at 05:01 PM.

  9. #9
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Excute Code Before Saving Workbook

    Jim, this is the issue you run into when trying to check for a specific color. The colorindex value of 6 is a specific shade of yellow from the standard colors toolbar. In Excel 2007 and newer, when you shade a cell "yellow" it may not be that specific colorindex value. Typically it's a shade of yellow and has different paramenters. This is why it's probably going to be easier to just check for blanks in those cells rather than mess around with colors.

  10. #10
    Forum Contributor
    Join Date
    06-24-2006
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Excute Code Before Saving Workbook

    Real Nice Guy got your code working thanks I had it in the wrong place - Barry still no luck with yours...

  11. #11
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Excute Code Before Saving Workbook

    Yes so did I Jim
    For what its worth I've attached my test sheet for you to test
    1. I'm using 2007 like you
    2. I'm using the Home menu tab; Font group; Fill color picker; Standard Colors and select Yellow swatch
    3. Its just a bog standard procedure that can go in a standard code module and run it 'manually'.

    It does bear out the advice given by Paul though. If you need to make your project portable - its good advice?
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    06-24-2006
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Excute Code Before Saving Workbook

    Barry does not work for me your test workbook. I am in fact running Excel 2010 (I know I will update my profile) shoudl this make a difference?

  13. #13
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Excute Code Before Saving Workbook

    Hi Jim
    Don't know about the nuances of different versions of Excel myself.
    But its likely it will make a difference.
    The Fill color picker represents the colors of a Theme.
    I had the Theme called "Office" selected at the time.
    That said I did select from Standard Colors which you would think are ..er..standard!

    I have attached a nice little routine provided by Marcol in an earlier post which you may like to play around with.
    Good luck mate - it can be very frustrating at times!
    Attached Files Attached Files

+ 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