Conditional data validation for MS Excel - excel

i read some tutorials that help out with conditional (or dynamic) data validation, but its usually dynamically generating list item choices based on previous selection etc.
what i want is, lets say i have
columnA : columnB
telephone_number : 911
name: peterparker
is it possible for me to dynamically specify data validation rules on columnB based on what is selected in columnA? if its telephone_number, i want to check that the corresponding cell in columnB is a natural number, and if it is a name,i want to check for a string for a certain length. of course i limit the string options that the user can input in columnA.

Following formula on custom validation rules will do:
=IF(A1="telephone_number";ISNUMBER(B1);IF(A1="name";LEN(B1)=8;TRUE))
This is example for cell in B1. You may choose a wider range and combine $ to adapt you formula to all range.

An alternative to data validation is Conditional Formatting. The benefit of the latter is that it is more versatile. We can have the following setting:
Then if we define a name, DynamicValidation as follows:
the formula at the validation box is
=EVALUATE(SUBSTITUTE(VLOOKUP(Sheet1!$A2,Sheet1!$D$2:$E$3,2,0),"()","("&ADDRESS(ROW(),2)&")"))
EVALUATE cannot be used in any sheet cell, but it can be used in Named ranges. However, you will have to save your book as macro-enabled, because it is a legacy macro, originating from Excel 4.0. What it does is the following:
Looks at the value in List1 and finds the formula that needs to be checked (VLOOKUP).
Subtitutes "()" with the cell value of interest (and adds back the parentheses).
Then if you open a conditional formatting box (Home tab, Conditional Formatting, New Rule, Use a formula to determine....) and input the formula =Not(DynamicValidation) like this:
then each cell that does not adhere to the formula will turn red. I tried that with data validation as well, but it does not work unless we build the depencency tree every time we change List 2.. I am not sure why.. But only conditional formatting works :) Here are some screenshots:
The benefit is that we can change the validation criteria without hard-coding.
I hope this helps!

Related

How to find all cells containing a formula that contains a specific constant?

let me show you an example of what I'm looking for. I have some constants defined in my excel workbook.
For example two contants mght be
Diesel_density = 0,84
Total_volume = 550
In another sheet I have some cells containing formulas like
A1=Diesel_density*100
A2=Diesel_density*Total_volume/100
I am looking for a formula that allows me to set up a cell in which I can type a string or value and it highlights (with conditional formatting or similar functions) all the cells that have that string/value in their formula. Please note that I am not looking to verify the presence of the string in the cell value, as all cell values will be numbers, but in the cell formula.
In the example I made, if I was to type "Total" in the "search cell", I'd like it to highlight the A2 cell only.
The purpose of this is to be able to check which cells values will be affected if one of the constants set in a specific sheet will be changed. For example, if Total_volume is changed to 600, alot of cells will contain that constant, and it is not possible to check it one by one, so I need an automatic function for this task.
Note that the Trace dependent function doesn't work in this case, since the cells using the named constants are in a different sheet that the one where all constants are defined.
Basically I need something that does what the simple Find tool already does since it looks for the text to search also in the formulas, but I need something that highlights all cells containing that string in their formula at once, without having to "find next" and look for each one.
Thanks!
What you're looking for is already available as a feature in Excel - called "Trace Dependents or Trace Precedents"
Steps:
Select the cell you want to check for references
got to Formula's tab in the Ribbon
Click "Trace Dependents" button
Additional Resources:
Official Documentation
Youtube Video
External Article

Excel conditional formatting row if cell starts with

I'm trying to format a row based off a cell within it.
My table runs from A6 to K207 and I want to highlight the row in bold if the cell in B6 starts with SA.
I tested my formula
=IF(LEFT(TRIM(B26),2)="SA","YES","NO")
in another cell and that works ok - but I can't get the formatting to work for some reason...
I removed the yes/no and placed into a conditional formatting rule applying to my whole table and it doesn't work. Even if I apply it to just a row it doesn't work.
Am I using if statements within conditional formatting right? I've looked on Google and here and have found similar issues but not the same one, they've all used set values/ranges but not a 'starts with' condition.
The conditional formatting is an IF statement by default, and formats depending on whether the formula is TRUE or FALSE therefore an additional IF statement is not needed:
=LEFT($A6,2)="SA"
This will do the trick, apply this to the entire range as needed.
It will test whether all cells in column a start with "SA" and highlight that cell. If it is applied to a range including more columns, the $ sign fixes the column as A so tests that cell and highlights every cell based on that, effectively highlighting the entire row.
You can also use an OR statement to pass multiple arguments like so:
=OR(LEFT($A6,2)="SA",LEFT($A6,2)="BO")
Welcome to SO. When doing conditional formating (CF) in a formula, you need to apply references.
Switch between relative, absolute, and mixed references
Right now your formula is =IF(LEFT(TRIM(B26),2)="SA","YES","NO"). You are using relative references, so the CF rule will check according to their position. In A6 it will check the value of A6, and I guess you would like the rule to check the value always in column B.
So the first fix you need in yor formula will be (it needs more, hold on)
=IF(LEFT(TRIM($B26),2)="SA","YES","NO")
Now, it will check always the value in column B. But we need to fix something else. To make the CF based on a formula, you need to formulate something that returns a boolean value, True or False. Your values Yes or NO are strings (text), not boolean, so they don't work.
So try this formula and let's see if it works:
=IF(LEFT(TRIM($B26),2)="SA",TRUE,FALSE)
An IF Functions allows you to make a question where the answer must be yes/no (true/false) and, depending of the answer, Excel will do an option or another.
With CF rule based on a formula, you just need the question itself, you don't need to choose an option. So the short version of this formula, as #Plutian states in his answer, is just doing:
=LEFT(TRIM($B26),2)="SA"
Try to adapt this to your needs.

Check if value in selection is part of set of other values

In Excel I have the following setup:
My Data (=Selection):
A,B,C,D,E,F,G
3,88,64,2,102,5,7
7,8,13,55,203,4,6
1,4,5,8,12,113,99
A set/pool of numbers:
3,7,1,55,22
Now I would like to check if any value in the set/pool of numbers is
in the data (selection) and color code it if so.
I believe this should be doable with conditional formatting.
But I do not want to add a seperate conditional formatting for every number.
But rather use a formula or anything concise.
Thanks.
With your data set up like this:
You can use the following approach:
define a named range myPool referring to I1:I5
select A1:G3, add conditional fomatting rule, choose use formula to determine which cells to format and enter:
=COUNTIF(myPool,A1)>0
choose background color and confirm
This will highlight values from I1:I5. To make it more universal, you can change definition of myPool to (adjust worksheet name):
=Sheet1!$I$1:INDEX(Sheet1!$I:$I,COUNTA(Sheet1!$I:$I))
In this case the named range will dynamically grow when you enter additional numbers in I6, I7, ...

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!
Here's a basic outline that I want to turn into a better formatted answer later this week when I have more time.
Create a Data Validation rule. In my case, I referenced a list of data in another workbook.
Turn off the alert for invalid data, we'll use the conditional formatting to show the data is invalid.
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.
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.
If you are referencing a list in another workbook, then it is simpler to recreate the data validation rule as a conditional formatting rule.
If your conditional formatting starts at Cell A1 on Sheet1, and the list is contained from A1:A10 on Sheet2, then your conditional formatting rule would be:
=AND($A1<>0,COUNTIF(Sheet2!$A$1:$A$10,$A1)<1)
This would highlight the cell if its value does not appear in the source list, and is not blank.
Clearly, for non-list based data validation, you can come up with an equivalent formula in the same manner.
You could make things more robust by setting up a named range for the list which both the data validation and conditional formatting refer to, such that if the list range grows, you only need to edit it in one place (the Name Manager), and it will update both Data Validation and Conditional Formatting ranges.

Excel integer to boolean condition

Creating a spreadsheet that has a bunch of test cases and at the end says pass or fail.
all values right now are either "Pass" or "fail". I need to be able to have a conditional where I can type in like a number. if it is above 5 i need it to return a pass. if it is below 5 i need it to return a false. any solutions guys?
The following might be the way to go:
where you enter a value in B1, which is then used to condition on. It's usually easier and more transparent to have a column that contains the conditioned value that is separate from the original data.
Another option might be conditional formatting. In this case, you condition on the value in a cell. Here I've formatted the cells to display a green/yellow/red dot (traffic sign) depending on the value in the cell.:
with the output resembling
Another option is to change the formatting of the cell to a specific "Custom" formatting:
Read more about how to Create a custom number format on Microsoft's help page.
A final option (that is a bit more intricate) requires you to set multiple, mutually exclusive, conditional formatting of the cells as well as separate "Custom" cell formatting:
Note that the cell entry A4 has a value of 16, while the display is actually "Pass" (since it's greater than 8 (cell B1). The steps for obtaining this include:
Create a conditional formatting using the "Use a formula to determine which cells to format" option.
Specify the "Format values where this formula is true" as depicted above (removing the dollar signs inserted around A4 by default), while setting the format using "Custom":
Do the same for the other (mutually exclusive) condition by creating a new rule for the same cell ("Format values where this formula is true:" =A4<=$B$1 and setting the "Custom" format to "Fail").
Once both conditional formatting rules are set, change the "Applied to" field to fit your range:
=IF(A2>5,"Pass","Fail") Replacing A2 with the appropriate cell reference.

Resources