+ Reply to Thread
Results 1 to 6 of 6

VBA to prevent saving if any cells are filled with a certain color

  1. #1
    Registered User
    Join Date
    01-09-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    23

    VBA to prevent saving if any cells are filled with a certain color

    Hello! I am trying to write a macro that'll prevent an excel workbook from being saved if any of the cells in the active sheet are filled with a certain color. Can anyone help me out? thank you!

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,837

    Re: VBA to prevent saving if any cells are filled with a certain color

    Place this macro in the code module for ThisWorkbook. It uses the ColorIndex number 3 which is red. Change the number to suit your needs. It will detect only the first cell that is color filled. If you want to detect all of them, delete the 'Exit Sub' line. If you do this and you have many that are color filled, you will get a warning for each cell. It also will prevent the workbook from being closed if any cell is color filled otherwise the file could be closed without saving it and you would lose any changes made to the data.
    Please Login or Register  to view this content.
    Last edited by Mumps1; 01-29-2014 at 12:55 PM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    01-09-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: VBA to prevent saving if any cells are filled with a certain color

    Thank you for your reply! I've copied this into my workbook code module and it doesn't seem to be working. I have the color fill happening by conditional formatting, and I have another BeforeSave event happening in my code already. Would either of these things be causing an issue?

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,837

    Re: VBA to prevent saving if any cells are filled with a certain color

    Conditional formatting does present a problem. Provided that you have only one conditional formatting rule, the following should work. Again, you may have to change the "255" (RGB red) to suit your needs.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-09-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: VBA to prevent saving if any cells are filled with a certain color

    I'm not sure what you mean by only one conditional formatting rule. I have 5 or 6 rules applied to the sheet but they all result in the same conditional format of a red fill in the cell. I would like the user to be alerted if any of these are triggered. When I copied and pasted your code I got the following error: Run-time error '9': Subscript out of range. I'm not that familiar with VBA to trouble shoot it. Is this happening because I already have code written for a separate before save event? thanks again for your time, much appreciated.

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,837

    Re: VBA to prevent saving if any cells are filled with a certain color

    I'm not sure what is happening. Picking up conditional formatting is tricky. I can't promise a solution but could you post a copy of your file and I'll have a look. If it contains any confidential information, you could replace it with generic data.

+ 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. Prevent save unless all cells or certain cells are filled in
    By cvo in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-29-2021, 02:16 PM
  2. Require input data in certain cells if other cells are filled before saving
    By Jenkins86 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2013, 08:07 PM
  3. Replies: 12
    Last Post: 07-10-2012, 05:04 PM
  4. How many cells filled with color?
    By codyaaron2 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-17-2009, 09:44 AM
  5. How to prevent cursor from moving behind filled cells
    By Spreadsheet Slave in forum Excel General
    Replies: 1
    Last Post: 04-04-2005, 09: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