Excel return Cell Style in a fromula - excel

In Excel how do I return the Style name applied to a cell as a formula?
When i use the =CELL("Color",AA11) (see below) it doesn't return a colour value, it still thinks it is unformatted, none of the other =Cell() functions seem to return style info either.
These are the styles as given by excel, I've marked a bunch of fields as good, bad or neutral. Ideally I'd like to be able to return 'Good' or 'Bad' etc into a cell.
Thanks

Here is a very easy UDF:
Function GetStyle(rng As Range)
GetStyle = rng.Style
End Function
Call in your cell B2 like =GetStyle(B2)
Edit
It's good to realize a UDF wont recalculate when you are changing the format of a cell!

Related

Return custom format of a cell (i.e. [$EUR ]#.##0,00;[Red][$EUR ]-#.##0,00)

is there a way to use VBA (or some excel function) to return a custom format that is used?
For example:
in cell A1 I have a value of 100 with custom format [$EUR ]#.##0,00;[Red][$EUR ]-#.##0,00
in cell A2 I have a value of 100 with custom format [$PLN ]#.##0,00;[Red][$PLN ]-#.##0,00
So basically, in A1 I have red colored EUR -100,00 and in A2 I have red colored PLN -100,00
Now let's assume I want do some calculations with only those cells that are formatted as EUR (in example above it means the cell has [$EUR] inside it's custom format.)
Any ideas on how to get that custom format string or anything else that will help me to differentiate those two cells by their custom format?
I tried Excel formula CELL("format";A1) and CELL("format";A2), but they both return ,2-
I've seen many VBA solutions to format a cell, but I haven't found any VBA function that returnes the custom format used (there are ways to return some general stuff like color, is it number etc, but I didn't find any custom format returns).
If you want to read that cells formatting (custom or not) then use:
Range("A1").NumberFormat
If you want a small UDF, that you can call from the worksheet itself, you need a small amount of VBA:
Create a new module, if you don't have one.
Paste into it the following:
Function formatting(rng As Range) As String
Application.Volatile
formatting = rng.Cells(1, 1).NumberFormat
End Function
Note, this will always return the formatting of the first cell within the range.
Then, in any given cell you can call that function- e.g.
=formatting(A1)
From there, you can use normal text manipulation functions such as Left, Mid etc. to locate the colour code or currency symbol - e.g.
=IF(LEFT(formatting(A1),2)="[$",MID(formatting(A1),3,3),"No currency found")
would give the result EUR

Change cell formatting

How can I change formatting of the cell that uses vba function from the code of that function?
Example I tried:
made vba module (see code below)
put in excel sheet in some cell "=test()"
function "works" - it changes cell value and shows 2 popup windows. But formatting stays the same
Function test()
MsgBox (Application.ThisCell.NumberFormat) ' shows "General"
Application.ThisCell.NumberFormat = "Currency"
'Application.ThisCell.NumberFormat = "#,##0_);[Red](#,##0)"
MsgBox (Application.ThisCell.NumberFormat) ' still shows "General"
test = 12345.6
End Function
How to make it work?
(I need custom formatting rule, not "currency", but custom rule (test example in commented line) doesn't work too)
Please don't kill me for this wacko solution :-)
I have created a function, resulting in the number 4096:
Function very_weird_UDF()
very_weird_UDF = 4096
End Function
Then I have created a conditional formatting rule, based on the present of that UDF (User-Defined Function) inside the formulatext of that cell, as you can see here:
Conditional formatting's formula: =FIND("very_weird_UDF",FORMULATEXT((B2)))
Screenshot of what it looks like:
For your information:
Cell "C3" contains the formula, the other cells in B2:C5 just contain the value 4096.
Cells E2:F5 contain the mentioned =Find(...) formula.
Oh, if you wonder why I use such a weird name for the UDF? Just imagine I used a simpler name, like 'a' or 'f' or something like that. That might highlight a lot of cells who don't use that formula (like a simple =If(...) or an =And(...) or ...

Conditional Formatting - highlight cells where formula is different?

Is there a way via Conditional Formatting (preferably no VBA, but if it's a must, then I'm open to it), to highlight a cell where the formula "idea" is different than the cell above?
I have a column of formulas, but have to manually edit a few of those. I'd like to have those manually edited formulas highlighted, so when I change the formula for the other cells, I know which cell to skip when updating that column.
For example, here's my column and formulas:
I'd like to have B5 highlighted yellow, since the formula is different.
I've tried using =FORMULATEXT($B3)<>FORMULATEXT($B2) but that doesn't work, since it's looking at the literal formula text...in which has they're always different. (=FORMULATEXT(B3)=FORMULATEXT(B2) will always be FALSE since the formula is technically changing, despite it being the same "idea").
I could also perhaps use =LEN($B3)<>LEN($B2) but that would have a false positive when the row changes from 9to 10, and again from 99 to 100...
The other option would, of course, just be to work in an IF()statement to clarify why I'm doing a different formula, i.e. =IF(ROW()=5,A5+A4+A2+A1,A5+A4) and use that...but there's no real logic for why I have to edit manually I could work in - which is why I'd just like a nice visual reminder on those random cells that the formula isn't like the others.
Edit: Quick note that the above formulas are way simplified. My actual ones are a little complex. I'm looking for a general answer to this too. Just thinking for my purposes, I could maybe do a check that if the formula has more than two + in it, highlight the cell. ...but I'm interested in a general way to solve this type of issue that could apply more broadly.
Here's another option for UDF:
Function findDifferent(Rng As Range) As Boolean
findDifferent = Not (Rng.FormulaR1C1 = Rng.Offset(-1).FormulaR1C1 Or Rng.FormulaR1C1 = Rng.Offset(1).FormulaR1C1)
End Function
Here's a quick VB aided solution I came up with. If I add a comment to the special cells (which I do to explain the formula/why it's different), I can check for a comment then highlight it.
Add this function to the workbook:
Function has_Comment(cel As Range) As Boolean
has_Comment = False
If cel.Comment.Text <> "" Then
has_Comment = True
End If
End Function
Then a simple Conditional Formatting formula of:
=has_comment(B2)
That works, and is relatively simple.
Edit: I found also you can do this, which doesn't rely on a comment. Just points out an Inconsistency Error.
Function has_inconsistency(cel As Range) As Boolean
has_inconsistency = False
If cel.Errors.Item(xlInconsistentFormula).Value = True Then
has_inconsistency = True
End If
End Function

Conditional formatting using the INDIRECT function fails with boolean AND or OR or with cells containing formulas

I have the following function for checking whether column L contains the word "completed" and I use INDIRECT to be able to color the whole row with Conditional Formatting:
=INDIRECT("l"&ROW())="completed"
This function works. However, I need to extend this, I want to use Conditional Formatting based on an extra cell as well, so I tried this:
=AND(INDIRECT("l"&ROW())="completed";INDIRECT("m"&ROW())="duplicate")
When I use this second function inside the Excel worksheet they give the proper TRUE or FALSE.
Furthermore, I needed a Custom Formatting on the result of a formula in a cell. I tried the following:
=INDIRECT("n"&ROW())=123456
This only worked if I removed the formula in the cell with the result itself as a number. Again, the function worked when pasted in an worksheet cell.
Is there a way to make this work inside Excel or is there a limit to what Conditional Formatting functions can do?
In case you ask: AND(1;1) works and makes everything yellow, AND(INDIRECT("n"&ROW())=123456;1) does not work, nor does replacing AND with OR.
The semicolon is because I am in the Dutch locale. Replace it with a comma if you are in an English locale.
Not sure why this wouldn't work in Conditional Formatting. But you can simply replace the AND function with * such as:
=(INDIRECT("l"&ROW())="completed")*(INDIRECT("m"&ROW())="duplicate")
You have to think in terms of xlR1C1 formulas to understand CFRs. A CFR based on a formula thinks of it as =RC12="completed" or more completely =AND(RC12="completed", RC13="duplicate").
The xlR1C1 formula does not change no matter what cell you paste it to; it is in this way that CFRs can be applied to a wide range of cells without expending calculation cycles to update the formula for each individual cell. RC12 means 'the cell in column L on the row you are on'. It does not change if filled down, filled right or copied to any other location.
Now unless you are actually working in xlR1C1 (File, Options, Formulas, Working with Formulas, R1C1 reference style) you have to convert the xlR1C1 to xlA1 style. If you are applying the CFR to a number of rows starting with the first row then the R becomes 1 and the C12 becomes $L.
'xlR1C1
=AND(RC12="completed", RC13="duplicate")
'xlA1
=AND($L1="completed", $M1="duplicate")
If you were applying the CFR to a range starting in row 2 change the $L1 to $L2 and the $M1 to $M2.
Among other reasons for not putting the xlR1C1 style formula directly into the CFR creation dialog when working in xlA1 style is that there actually is a RC12 cell in xlA1.

Use cell's color as condition in if statement (function)

I am trying to get a cell to perform a function based on the hilight color of a cell.
Here is the function I currently have:
=IF(A6.Interior.ColorIndex=6,IF(ROUNDDOWN(IF(M6<3,0,IF(M6<5,1,IF(M6<10,3,(M6/5)+2))),0)=0,0,ROUNDDOWN(IF(M6<3,0,IF(M6<5,1,IF(M6<10,2,(M6/5)+2))),0)),IF(ROUNDDOWN(IF(M6<7,0,IF(M6<10,1,M6/5)),0)=0,0,ROUNDDOWN(IF(M6<7,0,IF(M6<10,1,M6/5)),0)))
Just so you don't have to read through all of that, here's a more simple example
=IF(A6.Interior.ColorIndex=6,"True","False")
All that his is returning is #NAME? . Is there any way that I can do this as a function in a cell or is VBA absolutely required?
Thanks,
Jordan
You cannot use VBA (Interior.ColorIndex) in a formula which is why you receive the error.
It is not possible to do this without VBA.
Function YellowIt(rng As Range) As Boolean
If rng.Interior.ColorIndex = 6 Then
YellowIt = True
Else
YellowIt = False
End If
End Function
However, I do not recommend this: it is not how user-defined VBA functions (UDFs) are intended to be used. They should reflect the behaviour of Excel functions, which cannot read the colour-formatting of a cell. (This function may not work in a future version of Excel.)
It is far better that you base a formula on the original condition (decision) that makes the cell yellow in the first place. Or, alternatively, run a Sub procedure to fill in the True or False values (although, of course, these values will no longer be linked to the original cell's formatting).
I don't believe there's any way to get a cell's color from a formula. The closest you can get is the CELL formula, but (at least as of Excel 2003), it doesn't return the cell's color.
It would be pretty easy to implement with VBA:
Public Function myColor(r As Range) As Integer
myColor = r.Interior.ColorIndex
End Function
Then in the worksheet:
=mycolor(A1)
Although this does not directly address your question, you can actually sort your data by cell colour in Excel (which then makes it pretty easy to label all records with a particular colour in the same way and, hence, condition upon this label).
In Excel 2010, you can do this by going to Data -> Sort -> Sort On "Cell Colour".
I had a similar problem where I needed to only show a value from another Excel cell if the font was black. I created this function:
`Option Explicit
Function blackFont(r As Range) As Boolean
If r.Font.Color = 0 Then
blackFont = True
Else
blackFont = False
End If
End Function
`
In my cell I have this formula:
=IF(blackFont(Y51),Y51," ")
This worked well for me to test for a black font and only show the value in the Y51 cell if it had a black font.
The only easy solution that I have applied is to recreate the primary condition that do the highlights as an IF condition and use it on the IF formula. Something like this. Depending on the highlight condition the formula will change but I think that should be recreated (es. highlight greater than 20).
=IF(B3>20,(B3)," ")

Resources