Ok, so I am using excel 2007, and am trying to do some fairly complicated conditional formatting. Consider the following:
=1=1
Highlights the cell.
=INDIRECT(ADDRESS(2,COLUMN()))=5
Highlights cells in a column where the second row has 5 in it.
=AND(1=1,2=2)
Highlights the cell.
=AND(INDIRECT(ADDRESS(2,COLUMN()))=5,1=1)
Highlights nothing?
It seems that any complicated things I can do simply don't work. Even when the expression evaluates true when put into the cell directly, it doesn't always make the formatting work correctly. Has anybody else experienced this? Anybody have a workaround?
I get the same results on Excel 2003. Even your second example (which is missing the "=" BTW) has strange behaviour, since not all the cells in the column are highlighted until I minimize and restore the window.
I was able to get it to work by defining a name "foo" as "=INDIRECT(ADDRESS(2,COLUMN())" and then using "=AND(foo=5,1=1)" for the condition.
To define a name, use Insert->Name->Define, type "foo" (or whatever) in the Name box, and the formula in the Refers To box. Normally a named range refers to a fixed cell, but it can also refer to a constant, a formula, a range, etc. See Chip Pearson's site for details.
In Excel 2007, it would not accept my conditional format of
=AND(expr1, expr2)
where expr1 and expr2 were fairly complicated.
Just by adding an extra comparison, it started taking it.
=TRUE = AND(expr1, expr2)
I suspect it is validating for the presence of some basic operator, and it would not accept the condition without it.
Related
What I am trying to do with excel is a bit unorthodox, but I need a method of cleaning the data people import in to TechExcel's DevTrack. DevTrack creates values for fields if what you attempt to import is not 100% accurate to what exists already in the project, leading to duplicate values with slightly different formatting.
What I have come to is having a "validator" excel sheet; people copy/paste what they want to import, and anything that doesn't match a data validation table is highlighted.
Data validation itself does not work because copy/paste overwrites it and it is a waste of time to enter data manually.
I have managed to get conditional formatting working partially using this formula:
=ISERROR(MATCH(C5,D:D,0))
However it is not case sensitive. I need case sensitivity.
So I managed to create another formula which shows true if there is no match, however exact requires it to be an array formula which doesn't seem to work with conditional formatting.
So this works if it is entered in to a cell with ctrl+shift+enter, but doesn't work as a conditional rule.
{=ISERROR(MATCH(TRUE,EXACT(C5,D:D),0))}
Here is a simplified version of what I'm looking at for testing. That formula is true when there is no case match, but I can't enter it in conditional formatting with ctrl+shift+enter. If this worked as I want it to, the lower case "melon" would highlight red.
Is there a way to get this working, or is there an alternative method to do this?
If it matters, I'm using the latest version of excel in office 365.
Use a function that produces array processing without CSE. Examples are SUMPRODUCT or AGGREGATE (among others).
'to show TRUE for case-sensitive matches
=SIGN(AGGREGATE(15, 6, ROW($D$5:$D$9999)/EXACT($C5, $D$5:$D999), 1))
'to show TRUE on no case-sensitive match
=ISERROR(AGGREGATE(15, 6, ROW($D$5:$D$9999)/EXACT($C5, $D$5:$D999), 1))
The SIGN wrapper is unnecessary but for me it is a visual reminder that I'm looking for a boolean.
You cannot use dynamic ranges like D5:INDEX(D:D, MATCH("zzz", D:D)) in a CFR and you do not want to use full column references in AGGREGATE (or SUMPRODUCT) so I opted for $D$5:$D$9999.
I'm trying to color a cell red when the contents matches (at least) one of two other cells.
I've been trying this:
=AND(COUNTIF(CB17:CE17;CB55)=1,COUNTIF(CB18:CE18;CB55)=1)
and this:
=AND(COUNTIF(CB17:CE17;CB55)=1,COUNTIF(CB18:CE18;CB55)=1) = TRUE
but Excel doesn't recognize this as a formula ("There's a problem with this formula. Not trying to type a formula? ...")!
I can't get any conditional formatting formula to work with the AND operator it seems (and my Excel is the English version).
Any advice?
Edit:
some screenshots to clarify:
Error I get when using AND or the '+' operator in the formula:
Current contents of the criteria cells:
AND will be true of the expression inside results to true.
First of, you do not need to evaluate to 1 for AND to work. Secondly, you used ; and , instead of ;. You mentioned changing them to ; solved it.
So =AND( COUNTIF(...), COUNTIF(...) ) would already be enough.
Your formula would be =AND(COUNTIF(CB17:CE17;CB55);COUNTIF(CB18:CE18;CB55))
Now that being said, you want it to work for either of the values being true, that means you need an OR operator, not AND. The syntax is basically the same, just replace AND with OR. Your formula would be =OR(COUNTIF(CB17:CE17;CB55);COUNTIF(CB18:CE18;CB55))
Now since your ranges are CB17:CE17 and CB18:CE18 and they both evaluate to CB55, you could of course simply use =COUNTIF(CB17:CE18;CB55) which would be a lot simpler. I have explained the AND so you can learn from it, in case you really need to use more ranges and/or evaluate against different cells.
Can't believe I don't know this, but is there a way to avoid repeating a formula in an if statement if the logical test is dependent on it?
i.e.
=IF((SUMIFS formula)=0,"",SUMIFs formula)
I want to replace that SUMIFS function in the false scenario with something short that will tell it to just programmatically repeat the formula it originally tested for. Repeating the formula twice has to have detrimental effects on processing speed. Negligible, maybe, but want to go for best-practices here. Thanks.
You can force an error like #DIV/0! and then use IFERROR, e.g.
=IFERROR(1/(1/SUMIFS_formula),"")
You can assign a Name to a formula and use the Name..............See:
Assigning a name to a formula
Relevant excerpt -
For example, let's suppose we frequently use a formula like:
=SUM(A1:A100)-SUM(B1:B100) and this resides in A101 and is copied across many columns on row 101. It would be better in this case to
create a custom formula that does this in each cell on row 101. Here
is how;
1) Select cell A101 (this is vital).
2) Go to Insert>Name>Define and
in the "Names in workbook" box type: SalesLessCosts
3) Now click in
the "Refers to" box and type: =SUM(A1:A100)-SUM(B1:B100) then click
Add.
Now you can replace the formula in cell A101 with: =SalesLessCosts.
You can also copy this across row 101 and it will change its relative
references just as the formula =SUM(A1:A100)-SUM(B1:B100) would. The
reason it does this is all down to the fact we selected A101 before
going to Insert>Name>Define and used relative references in
=SUM(A1:A100)-SUM(B1:B100) when we added it to the "Refers to" box.
If all you need to do is hide zeroes, there is an easy way:
Select all cells where you wish to hide zeroes
Go into Custom Number Formatting
Set format to "General;General;"
The custom formatting has a structure of [positive numbers];[negative numbers];[zeroes]
By making the last part blank you are effectively hiding zeroes, but showing everything else.
The advantage over conditional formatting is that you can use this on any background.
A neat trick which I sometimes use is to hide the cell value completely by using a custom format of ";;;". This way you can put images inside the cells, like the conditional formatting ones, and not see the value at all.
Try using the SUBSTITUTE function like this :
=SUBSTITUTE( VLOOKUP( H4; $D$5:$E$8; 2; 0 ); $H$1; $I$1 )
Here is an example:
Here the formula I don't want to repeat twice is the VLOOKUP function.
The result of VLOOKUP is a string found in another table (ex : "Green").
I want to check if that string matches a specific string value in $H$1 (here, "Yellow").
If it does, SUBSTITUTE replaces it with$I$1 (the error string you want. Here, "FORBIDDEN").
If it doesn't, it displays the VLOOKUP result string (the normal authorized output, like "Green").
This is useful for me because my actual formula is quite long, so I don't want to write it twice.
I also dont want to use two different cells, because I'm already applying this formula on 10 columns, meaning I should add an extra 10 columns to make it work.
In some scenarios, MAX() or MIN() can do a wonderful job.
E.g., something like this:
=IF(SUMIFSformula>0,SUMIFSformula, 0)
Can be shortened to this:
=MAX(0,SUMIFSformula)
The LET formula can be used for this exact scenario. You can define the formula as a variable and then within that same cell you can reference the variable in your formula.
The LET formula format looks like this:
=LET(name,name_value,calculation)
SUMIFS Example
Here's how it would work with your SUMIF example so that you don't have to repeat the formula:
In this screenshot we have an array A1:B7. We want to sum the values (Col B) if the name in ColA is "apple".
For this we have a standard SUMIFS formula of
=SUMIFS(B1:B7,A1:A7,"apple")
The formula is showing in E2. The result is shown in E3.
To put this into the IF statement without having to repeat the formula we can use LET as shown in the screenshot.
We create a variable with the SUMIFS formula as the value of that variable. We then write our IF statement using the variable name instead of rewriting the formula multiple times.
=LET(name,name_value,calculation)
Variable name: sumapples
Variable value: SUMIFS(B1:B7,A1:A7,"apple")
Calculation: IF(sumapples=0,"",sumapples)
Put together in the LET function it looks like this:
=LET(sumapples,SUMIFS(B1:B7,A1:B7,"apple"),IF(sumapples=0,"",sumapples))
This LET function can be used in any Excel formula, and is very useful for shortening long formulas that have repetition.
Optional: Extra complexity
If you want to you can get extra complicated by naming multiple variables.
=LET(name,name_value,name2,name_value2,calculation)
Since Excel 2007, the IFERROR statement does what the OP asked. From the help file:
Description:
Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula. [italics mine]
Syntax:
IFERROR(value, value_if_error)
I've since realised that this was already answered by #barry houdini above.
Here is a hack - depending on whether you are just interested in the displayed value, or whether you need to use the value in another formula:
Put your SUMIF formula in the cell (without the IF part)
Create a conditional formatting rule which sets the font color to the background color when the cell value is 0
And hey presto, you get the desired result.
As I said - it's a hack, but it does prevent the double evaluation.
There is no "clean" solution that I am aware of.
I'd like to color cells according to their value (cf conditional formatting - color scales) in the style menu of Excel 2007 to newer.
It works fine and the effect is wonderful when you have a wide range of data with fairly distributed values.
However, the column I am formatting conditionally is the result of some computation. Sometimes I get N/A values because one of my input is N/A, and sometimes I get the exception "division by zero" as my cells are the result of a division. In these scenarios, i don't get any colors at all.
Is there a way to ignore these problematic cells that happen sometimes, leave them blank and process colours for the rest of them ?
Thank you guys ! ;)
A picture of when it works fine :
Another picture showing a problematic cell cancelling the colouring :
I'm looking for an Excel solution to the problem, which I can then record as macro to port to my C# application. But if someone has a nice workaround in C#, I'd be happy too (i.e. I use ToColourScalePercentile on the range, I was thinking to maybe remove problematic cells from the range somehow)
you could use IsError function to check if there is N/A and in case there is leave the cell empty, otherwise put the value there.
Since you are using 2007 you can wrap your calculation in the iferror() function would look like:
=iferror(your calculation here, 0)
This way if your caculation evaluates to an error it will substitute the second argument. You can use 0 or "" to have it blank.
I'm trying conditional formatting on a sheet. I need to fill the cells with a certain color according to the following conditional statement:
=AND((INDIRECT(ADDRESS(4;COLUMN()))>=INDIRECT(ADDRESS(ROW();4)));(INDIRECT(ADDRESS(4;COLUMN()))<=INDIRECT(ADDRESS(ROW();5))))
When I try the statements in the AND() function separately, they seem to work, but when I put them together in the function I don't see any formatting happening.
Here is some background:
Row 4 of the "current column" has a date (DATE1) in it. There are also dates on the D and E columns of the "current row" (DATE2 and DATE3). So, I would like to fill the cell with a color if DATE1 is between DATE2 and DATE3.
I cannot see why the formula is not working. Any help is much appreciated.
Update (Dec 13, 2011):
I implemented a function that I call from the cells I need this functionality. The function returns integer values. Then conditional formatting only uses the integers in the cells. This way, the conditional formatting is less complicated. I'm passing INDIRECT(ADDRESS(ROW();COLUMN())) into the function I implement. So, I have all the information I need when working on relative and/ or absolute cells. Would be great to know a simpler way to pass the current cell as range into the function.
Note: ActiveCell didn't seem to work for me. It uses the data from the cell which is selected by the time the function is run. That's not what I'm looking for. I could of course pass the cell itself (as in A4, B7, etc.) but I'm not sure if it really matters in terms of performance.
Thanks to all of you who responded to my question.
I was having the same problem with the AND() breaking the conditional formatting. I just happened to try treating the AND as multiplication, and it works! Remove the AND() function and just multiply your arguments. Excel will treat the booleans as 1 for true and 0 for false. I just tested this formula and it seems to work.
=(INDIRECT(ADDRESS(4,COLUMN()))>=INDIRECT(ADDRESS(ROW(),4)))*(INDIRECT(ADDRESS(4,COLUMN()))<=INDIRECT(ADDRESS(ROW(),5)))
You can use a much simpler formula. I just created a new workbook to test it.
Column A = Date1 | Column B = Date2 | Column C = Date3
Highlight Column A and enter the conditional formatting formula:
=AND(A1>B1,A1<C1)
I had a similar problem with a less complicated formula:
= If (x > A & x <= B)
and found that I could Remove the AND and join the two comparisons with +
= (x > A1) + (x <= B1) [without all the spaces]
Hope this helps others with less complex comparisons.
This is probably because of the column() and row() functions. I am not sure how they are applied in conditional formatting. Try creating a new column with the value from this formula and then use it for your formatting needs.
COLUMN() and ROW() won't work this way because they are applied to the cell that is calling them. In conditional formatting, you will have to be explicit instead of implicit.
For instance, if you want to use this conditional formating on a range begining on cell A1, you can try:
`COLUMN(A1)` and `ROW(A1)`
Excel will automatically adapt the conditional formating to the current cell.
I am currently responsible for an Excel application with a lot of legacy code. One of the slowest pieces of this code was looping through 500 Rows in 6 Columns, setting conditional formatting formulae for each. The formulae are to identify where the cell contents are non-blank but do not form part of a Named Range, therefore referring twice to the cell itself, originally written as:
=AND(COUNTIF(<rangename>,<cellref>)=0,<cellref><>"")
Obviously the overheads would be much reduced by updating all Cells in each Column (Range) at once. However, as noted above, using ADDRESS(ROW(),COLUMN(),n) does not work in this circumstance, i.e. this does not work:
=AND(COUNTIF(<rangename>,ADDRESS(ROW(),COLUMN(),1))=0,ADDRESS(ROW(),COLUMN(),1)<>"")
I experimented extensively with a blank workbook and could find no way around this, using various alternatives such as ISBLANK. In the end, to get around this, I created two User-Defined Functions (using a tip I found elsewhere on this site):
Public Function returnCellContent() As Variant
returnCellContent = Application.Caller.Value
End Function
Public Function Cell_HasContent() As Boolean
If Application.Caller.Value = "" Then
Cell_HasContent = False
Else
Cell_HasContent = True
End If
End Function
The conditional formula is now:
=AND(COUNTIF(<rangename>,returnCellContent()=0,Cell_HasContent())
which works fine.
This has sped the code up, in Excel 2010, from 5s to 1s. Because this code is run whenever data is loaded into the application, this saving is significant and noticeable to the user. It's also a lot cleaner and reusable.
I've taken the time to post this because I could not find any answers on this site or elsewhere that cover all of the circumstances, whilst I'm sure that there are others who could benefit from the above approach, potentially with much larger numbers of cells to update.
Same issues as others reported - using Excel 2016. Found that when applying conditional formulas against tables; AND, multiplying the conditions, and adding the conditions failed. Had to create the TRUE/FALSE logic myself:
=IF($C2="SomeText",0,1)+IF(INT($D2)>1000,0,1)=0