+ Reply to Thread
Results 1 to 12 of 12

Validating cells based on content and background color

  1. #1
    Registered User
    Join Date
    04-24-2015
    Location
    Seattle, WA
    MS-Off Ver
    2013
    Posts
    12

    Validating cells based on content and background color

    Hello,

    I'm not quite sure where to begin, so thought I'd jot down what I am trying to do to see if anyone has any thoughts:

    1. I have a workbook with a worksheet named Sheet1
    2. I have VBA setup to change the background color of required cells to yellow ((i.e. Interior.ColorIndex = 6)

    I would like to do the following before allowing a user to save the workbook:

    1. Check the entire worksheet for to see if there are any cells with with the yellow background color that arenull

    Maybe it'd look something like this?:
    Please Login or Register  to view this content.
    The reason I have selected "A:DD" is because those are the columns that could potentially contain cells with yellow backgrounds

    Any help you can offer is appreciated!

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Validating cells based on content and background color

    Sheets("Sheet1").range("A:DD").Interior.ColorIndex = 6 -> That won't work unless the entire range shares the same color. What you are going to have to do, is either check each cell individually OR come up with an alternative (like below).


    1)
    Question - how are you finding the cells to turn yellow in the first place? - You could a named formula to remember that range (call it required_cells or something).
    Then your final check will be much easier as you can just check that range - still one cell at a time, but you will greatly reduce the number of cells to check.

    2)
    Use some kind of special cell to hold the number of required cells (like off to the side, call it 'RequiredCount' or something). 'RequiredCount' will hold a value, say 10 for example. Use another cell, call it 'CountActual' to hold something like =COUNTA(A1, B3, Y7...) where those made up addresses are the actual references to the required cells (you'll have to populate these special cells when you make the required cells yellow).

    IF those 2 cells don't equal each other - there is missing information from one or more required cells... Have your before save macro just check the numbers.

    Make sense?
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Validating cells based on content and background color

    Does this do what you are looking for?
    Please Login or Register  to view this content.
    Some people volunteer in soup kitchens or hospitals. I choose to make the world better by trying to help you with Excel. We're all learning.

    <---Click * Add Reputation for all helpful comments. It's like giving a smile.
    Forum Rules: How to mark your post [Solved] and have a happier, Excel enriched life.

  4. #4
    Registered User
    Join Date
    04-24-2015
    Location
    Seattle, WA
    MS-Off Ver
    2013
    Posts
    12

    Re: Validating cells based on content and background color

    Hi GeneralDisarray,

    Listed below is some code that changes the interior.colorindex to yellow based on a couple of conditions:

    Please Login or Register  to view this content.
    Depending on the value of column 94 and 95, the interior.colorindex could be black, yellow or xlNone. If it's black or xlNone, entry shouldn't be required. Only if there is a yellow background.

    In your first suggestion, what would the named formula look like?

    Thanks, so much, for your help!
    Last edited by anthronewman; 08-04-2015 at 04:54 PM.

  5. #5
    Registered User
    Join Date
    04-24-2015
    Location
    Seattle, WA
    MS-Off Ver
    2013
    Posts
    12

    Re: Validating cells based on content and background color

    Hi Nigelbloomy,

    Unfortunately this did not work. Thanks for the suggestion, though!

  6. #6
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Validating cells based on content and background color

    What about my code didn't work? I tested it on the attached file and it seems to work.

    Sorry. There was one line that only worked because of the way my sheet was set up. This should be better:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by nigelbloomy; 08-04-2015 at 05:38 PM.

  7. #7
    Registered User
    Join Date
    04-24-2015
    Location
    Seattle, WA
    MS-Off Ver
    2013
    Posts
    12

    Re: Validating cells based on content and background color

    Hi Nigel,

    This time it does work if the blank-yellow field is between (or contains) A1:DD1. Unfortunately, it doesn't seem any other rows, though. I'm looking at the possibility of clients filling out up to 1000 rows with information that will need to be validated.

    I really appreciate you having taken the time to help me out. Not sure if there is a simple way to have the code check all rows?

    Thanks!

  8. #8
    Registered User
    Join Date
    04-24-2015
    Location
    Seattle, WA
    MS-Off Ver
    2013
    Posts
    12

    Re: Validating cells based on content and background color

    Nigel,

    I adjusted the code and it seems to be working now. Don't know if it's ideal since it is going to check through cell D1000 every time, but it's definitely recognizing yellow cells beyond the first row

    Please Login or Register  to view this content.
    Thank you for helping with this!

  9. #9
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Validating cells based on content and background color

    The problem is the code Nigel posted doesn't match the code in the workbook he attached:

    Posted:
    Please Login or Register  to view this content.
    What was actually in the 'test()' sub:
    Please Login or Register  to view this content.
    That's why it was only checking the 1st row - but if you used what he posted it would only check the last row.


    You can just use the "UsedRange" method - you don't actually need to count it's rows - which will save you the need for DIM statements (and I don't think the WITH was needed):

    Please Login or Register  to view this content.

    SEE ATTACHED

    Again - this is not the best way to go, but if you want to actually check each cell in the used range for the conditions listed - that will do it.
    Attached Files Attached Files
    Last edited by GeneralDisarray; 08-05-2015 at 08:28 AM.

  10. #10
    Registered User
    Join Date
    04-24-2015
    Location
    Seattle, WA
    MS-Off Ver
    2013
    Posts
    12

    Re: Validating cells based on content and background color

    Thanks for clarifying on that, GeneralDisarray. The code you attached works great!

    So, I'm still gathering requirements from my manager, but it's likely that the "required" fields will be limited to about 30 - 35 columns (yes, that's still a lot, but much fewer than the A:DD range I've posted here. I'll have to do some more testing on performance, but as of now it is pretty fast. I may wind up limiting the number of rows a client can fill out to 500. At the end of the day, there will be just a few instances where clients will be filling out more than 100 rows, so we may be good with the code you provided.

  11. #11
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Validating cells based on content and background color

    Thank you General. I wasn't sure exactly how to use the usedrange method in this case. I didn't want to use the regular methods for finding the last row because I don't know where the data would be on the sheet and most only look at column A. Thanks for teaching me something today!

  12. #12
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Validating cells based on content and background color

    @nigel - no problem, and right-back-at-cha on previous postings you've shared.

    @OP

    Yeah, it wasn't terribly slow and may do the trick for you completely. I shy away from "just loop over every cell in the worksheet" methods as much as possible.

    I occurred to me while I was typing that previous sentence - that you could just check the blank cells in the used range, which would be less iterations through the loop.


    Adjusted code - just checks the blank cells in the usedRange to see if they are yellow:

    Please Login or Register  to view this content.
    I also added a 'cell.select' line to take the user to the required cells that caused the routine to exit in the first place.

+ 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. How to add cells just based on their color background?
    By meirelesj in forum Excel General
    Replies: 7
    Last Post: 07-28-2015, 07:04 AM
  2. Modify Cell Background Color based on cell content
    By CWDurkin in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-01-2014, 11:57 AM
  3. Change cell background color based on another cells background color
    By Queo in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 4
    Last Post: 06-10-2014, 05:28 AM
  4. [SOLVED] Sum and average cells based on background color
    By Kaitlynn in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-22-2013, 12:51 PM
  5. [SOLVED] Color Index to sum up cells based on background color
    By jph89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-16-2013, 03:23 PM
  6. Replies: 3
    Last Post: 05-02-2012, 09:08 AM
  7. Count Cells Based On A Background Color
    By NSTurk725 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-27-2010, 11:29 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