Excel conditional formatting - color scales - excel

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.

Related

Excel conditional formating - conditional on sum of all previous cells and total sum

I have a question with regards to conditional formatting which I simply can't seem to solve.
The aim is to format the background color for the left-most cells that are blank up until a cell contains a value - and after this there should be no more formatting in this row. You can see an image of the result I'm hoping for beneath:
So far I've managed to create the conditional formatting of the blank rows, but sadly haven't managed to create the "single-cell" formatting (in yellow) conditional of the sum of all the first cells being = 0.
I've created a formula which actually succeeds in calculating the sum of the previous cells, but this formula includes INDIRECT() which it seems that conditional formatting doesn't allow. At least I'm getting an error starting with the follow (translating the error from Danish to English might not be intuitive):
You cannot use reference operators such as.....
The formula I'm trying is the following:
=AND(SUM(A2:INDIRECT(IF(COLUMN(A2)>=27;CHAR((64-26)+COLUMN(A2))&CHAR((64-26)+COLUMN(A2))&ROW(A2);CHAR((64)+COLUMN(A2))&ROW(A2))))>0;$K2>0)
Where $K2 is the sum of the row.
Is there a way to SUM a range of cells by doing something similar to this:
=SUM(B2:CHAR((64)+COLUMN())&ROW();"")) to dynamically SUM the range from B2 to the current cell? The problem in this case is whether the part CHAR((64)+COLUMN())&ROW();"") can be converted into a legible cell such as B4 to make it work inside the =SUM() formula?
Sadly the =ADDRESS() formula cannot be used (as far as I know) as this will trigger the same error in conditional formatting as well.
For now I would like this to work with the regular Excel conditional formatting, but if anyone have a simple VBA this would also have interest - however, I would by far prefer the regular solution.
The question:
Is there a way to create a formula that doesn't trigger this error - maybe by refrasing the formula or doing something completely else?
It seems to me that this could be handled with a much simpler CFR formula like,
=and(not(sum($a2:a2)), $k2)
Am I missing something?

Excel: Use an if statement to return blank value for formulae and charts

Is there a way to have an if function return a value that will be ignored by both average functions and charts?
In Gnumeric, an open-source, Excel-like program, you can have an if function return "" and the cell will appear empty. If you take the average of a bunch of such cells, some with returned values and some with returned "", the "" will be completely ignored. And if you create a chart with those cells as data points, cells with "" will not have a point plotted.
However, doing the same thing in Excel doesn't seem to work. I've selected the "Show empty cells as: Gaps" option (described here) but it doesn't work. I think this is because the cell isn't technically empty.
Answers to similar questions suggest using "na()" in the if statement, but this messes with the averaging functions.
Does anyone know of a solution?
Note: While this subject area has been addressed before, I don't think this is a duplicate. Here are some similar questions:
IF statement: how to leave cell blank if condition is false ("" does not work)
Leave a cell blank if condition is false
Creating a chart in Excel that ignores #N/A or blank cells
Perhaps you could keep the #N/As (for the chart) but instead of using AVERAGE on it's own you could use the array formula (assuming the values you want to average are in A1:A5):
=AVERAGE(IFNA(A1:A5,""))
Once you've entered the formula press Ctrl+Shift+Enter to evaluate it as an array formula.
I think you would have to use a slightly more complicated function to calculate the average.
There may be a better way, but this will work (in conjunction with the na() method you mentioned in your question.
=SUM(IF(ISERROR(AVG_RANGE),0,AVG_RANGE)) / SUM(IF(ISERROR(AVG_RANGE),0,1))
This is an array formula so you'll need to commit it with ctl-shift-enter

Conditional formatting using AND() function

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

Conditional formatting to hide cell content even when printed

I would like to set up a conditional formatting setting that would hide the contents (the cell should look blank) if the cell's contents is equal to another cell. Does anyone know of a function to do this? I tried just making the font the same color as the background (gray, in this case), but unfortunately when this is printed, there is some sort of residue shadow left over from the text.
Here is what happens when I do gray on gray (they are supposedly the same color):
I am using Excel 2008 on the Mac.
Below in A2 we have the conditional formatting that hides the content of that cell if it is equal to B2.
In Conditional Formatting create the following rule: =$A$2=$B$2, then press Format...
On the Format menu, click Cells, and then click the Number tab.
Under Category, click Custom.
In the Type box, type ;;; (that is, three semicolons in a row), and then click OK.
another way around this is a hack but it should work:
have two sheets, data and view
base your conditional formatting on the view sheet off of values in the data sheet (not sure if this is an option...)
use if statements in each cell that say something like if(dataCell=matchCell, "", dataCell)
I suspect that the printing problem is not so much a problem with Excel, but a feature of the printer.
Printer manufacturers obviously want to get the "best" result that they can on a printed page. Some will optimise colour by doing things like making smaller volumes of print (e.g. charters and lines) a bit darker and large areas (e.g. cell backgrounds a bit lighter). This is so that when they are side-by-side they look like the same colour. However, when printed on top of one another as per your case, you get a residual effect.
This stems from a well known effect that for any given colour, a large area of that colour will look darker than a small area of the exact same colour. There are other effects that give this illusion - such as the background and nearby colours. For an example - that is not exactly the same as yours - but illustrates the illusion, search the web for "adelson's checkerboard" and read the explanations.
In any event, the solution proposed by mpalanco (use conditional formatting of three semi-colons) is the easiest way to achieve clean result - every time, printed to paper, printed to pdf, on screen and any other form.
You are on right track to resolve this. You just need to make the font color white this will not show the value when printed on paper unless the paper is not white. I have done this with Office 2007.
So I think that to get the effect that you would like, you will need to use a macro to check the contents of the cell. A worksheet_change event should work well.
To get this code functional, I will need you to tell me the range of cells whose values should be hidden, and the range of cells that we are checking the values against.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range.Address Then
For Each VariantValue in OtherRange
If Range.Value = VariantValue Then
Range.NumberFormat = ";;;"
End If
Next
End if
End Sub
http://www.extendoffice.com/documents/excel/919-excel-hide-cell-contents.html
http://www.ozgrid.com/VBA/run-macros-change.htm

Are Excel's Conditional Formatting Formulas Broken

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.

Resources