Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I base conditional formatting on validation results in Excel?

I want to change the formatting of a cell if the cell is not valid. In this case, "valid" means that the cell has failed the data validation rules.

I'm asking this question because I couldn't find the answer on SO. I eventually solved it. I'll post my answer and see if people want to comment or provide a better answer!

like image 453
Alkix Avatar asked Jan 06 '14 21:01

Alkix


People also ask

Can you use conditional formatting with Data Validation?

Excel's conditional formatting tool applies formatting depending on cells' contents. Validation rules can change cells font, style, size or border. They can also change the text's color.

How do you set conditional formatting in Data Validation?

Select the range D2:D20. On the Home ribbon click the Conditional Formatting icon drop down and select New Rule. When creating a formula for a range it is important to build the formula to work with the first cell in the range, in this case cell D2. The formula you create must return TRUE to apply the format.


1 Answers

Here's a basic outline that I want to turn into a better formatted answer later this week when I have more time.

  1. Create a Data Validation rule. In my case, I referenced a list of data in another workbook.
  2. Turn off the alert for invalid data, we'll use the conditional formatting to show the data is invalid.
  3. Add a conditional formatting option for the cells that have the data validation rule. To do this, go to Manage Rules -> New Rule, and in the formula, use =IS_VALID(CELL("row",C4), CELL("col", C4)), where C4 is the first cell you want to start entering data into.
  4. Create a custom function that looks something like

this:

Public Function IS_VALID(row, column) As Boolean
IS_VALID = Not Cells(row, column).Validation.value
End Function

Finally, you can set your conditional formatting effects to whatever you want, like coloring the cell red. This answer worked for me, and I wanted to not forget to add it to SO, but don't have the time to make it all pretty right now.

like image 176
Alkix Avatar answered Oct 14 '22 15:10

Alkix