Conditional formatting based on the content of a formula - excel

Long story short: My boss has an Excel spreadsheet for calculating product prices. She has three columns to calculate different margins (so would contain a formula like =cost*.5 etc.), then adds to those values to determine final retail (so would be =column+500). She has her margin columns highlighted different colors and wanted to automatically highlight the final retail cell based on which margin column it references.
I'm sure there's a way of setting up the document from the start that makes this more intuitive, but short of restructuring her entire system, are there any formulas I could use in combination to return a format based on the content of the formula?
I immediately assumed to use "if cell contains specific text" and use the referenced column letter as the text, but excel reads the output for the formatting and not the formula, so I am stumped.
I was thinking somehow to use "isformula", but all the values are formulas just with different input.

Suppose you made table of all the unique formulas that are used:
formula_test, rule,
=cost*0.5,1
=column+500,2
Then you could create conditional formatting rules by formula with one for each unique formula case.
=VLOOKUP(FORMULATEXT(A1),$A$2:$B$3,2,FALSE)=1
=VLOOKUP(FORMULATEXT(A1),$A$2:$B$3,2,FALSE)=2
Where A1 is the first cell in the range where you want the conditional formatting to be applied.

The missing link was "SEARCH":
=SEARCH("=Sum(J",FORMULATEXT(N2))
(J = the referenced margin column, N2 = first cell to apply the final retail formula)

Related

How to highlight four random selections from a column?

I'm trying to randomly highlight (change the cell colour) of four cells in the first Excel column. It is important the selections highlighted are only cells containing data as the size of the data set will vary.
The highlighted selections needs to be un-highlighted when the macro is run again to generate four new selections.
I have tried to brute force this with functions (RAND-RANK-XLOOKUP), but the outcome is messy and doesn't react to different sizes of data.
Highlighting the cells in a color is the goal, conditional formatting could be suitable.
Four random cells from the first column of the Excel sheet is the target (cells only containing data).
Using Conditional Formatting
Let's say you have data in column A. For example 16 rows like this:
You could use the COUNT() function to calculate the number of cells with content in column A in cell D1 :
=COUNT(A:A)
And then you could calculate the position of the cells you want to highlight with
=RANDBETWEEN(1,$D$1)
Then you can create a helper column to check if a cell should be highlighted.
Formula in G2 : =IFERROR(MATCH(CELL("row",A2)-1,$D$2:$D$5,0)>0,FALSE)
The idea behind this function is simply to check if the row of the data point corresponds with one of the random positions we have.
Finally, you would use a formula to define your conditional formatting by applying it to Column A and using =$G2 as the formula.
Note that:
The highlighing will reset everytime you change a cell provided that you have "Automatic calculations" turned ON.
There is a possibility that 2 or more of the random positions are the same, so you'll need to recalculate if that's an issue.

Conditional formatting: automatically refer to the next cell?

I have a schedule with team member names and the column headers are half hour time intervals. I want to shade the cells of hours each person does not work with gray according to their shift schedule, so that I know not to schedule that person during that hour.
I have created a separate table with each person's shift schedule, and the names appear in the same order as in the schedule.
Is there any way to conditionally format the cells at once? There has to be an easier way then what I am doing now...which is one by one clicking on each person's cell and creating the formatting formula.
I can't copy paste the formatting because the formula still refers to the previous person's shift on the other table. I need it to refer to the next row.
The formula I use for conditional formatting is:
='Job Functions'!$O$5>$C$9
Where Job Functions is the sheet that contains the shifts, O5 is the shift assigned to that employee, and C9 is the column header on the schedule (6:30am). I just clicked on the cell and created a new conditional formatting rule from the excel ribbon on top...no vba.
If there is a VBAsolution to this that'd be great! I'm fairly new to VBA
Conditional formatting works like this
Let's say I have an array of numbers in A2:E5 and a header row in A1:E1. I want to have my array of number be green if the value of the cell is greater than it's column header. That is to say I want to compare A2>A1, B5>B1, D4>D1, etc. this means I want the header row comparison to be constant.
In Excel formulas you use the $ symbol to maintain constant references. Since I want the row to stay constant but I want the column to be relative to the cell in my array of numbers my header reference will be A$1 (column is relative, row is locked).
This is just the formula used to determine if formatting will be applied or not. If it returns true then the conditional formatting is applied, if it returns false then nothing happens.
However, where the formatting is applied is determined by the Applies to reference. In my example below I am applying the formula A$1<A2 to $A$2:$E$5. This means that in the cell A2 the formula A$1<A2 is used to determine if formatting is applied, but in B3 the formula B$1<B3 is applied. This is the same logic as if you were to have dragged the formula itself into these cells.
If instead my Applies to formula were $B$2:$E$5 this means that B2 would be colored green if A$1<A2, and B3 would be colored green if A$1<A3.
So with all that your formula should probably be
='Job Functions'!O5>C$9
drag and drop it down to fill the other cells

Excel Help: Applying Conditional Formatting to Numbers in Percentage format

I am currently trying to apply conditional formatting to certain rows which contain numbers in percentage (e.g. F11 to N11; F14 to N14; F17 to N17 and so on). These are also the only rows in the worksheet that have numbers in percentage. Would like to know what the fastest way to go about doing this is.
Should I:
(i) apply conditional formatting to the whole worksheet to numbers in percentage (if possible) or
(ii) apply conditional formatting to each individual row which is extremely time-consuming?
Appreciate if anyone can help.
Thanks in advance! :)
If your data in the percentage rows is also formatted as percentage, you can use the =CELL("format",[reference]) function, that returns "P0" or "P2" for percentage formatting.
Use a helper column (which you can hide if you want) to check the formatting of the first/last cell in the row, and then set conditional formatting based on the column:
After you create the column, you set a conditional formatting rule on the the upper-left cell in the range based on the formula: =[top cell in helper column]="P0". Keep the column fixed with a $ but not the row, as in the screenshot below, so you can drag the formatting to all the range with correct reference to the helper column.
Next step is to drag this cell to all the range, choosing the "formatting only" option:
Finally, you have to format again all rows that contains percentage values to percentage formatting.
However, if the place of the percentage rows is fixed, you don't need that CELL() function, just write in the helper column something like p or 1 for the rows to format.
(I have also tried to plug the formula directly to the formatting condition, but it doesn't work well all the time)
Tell me if it solved your problem ;)

Validating cells in VBA excel

I am creating a spreadsheet which will be uploaded to a database and there needs to be strict validation.
I need to ensure that if cell A2 has text in it, then cells C2,D2,F2,G2 must have data.
I would like to do this with colour - if A2 has text then the other cells are highlighted red until they contain text, then the background colour changes back to white. (The cells listed are REQUIRED and must contain a value).
There will be about 1000+ rows in this spreadsheet and I have attempted this using the data validation but it would involve me going in row by row.
Data validation does not require you to do row by row, by default Excel seems to make it so (by fixing row and column with dollar signs), but that can be easily changed.
What you can do is create a formula in your conditional formatting for the top most row where you want this applied to, now get rid of the dollar signs in front of the row numbers in your formula and extend the range where this applies for to all the thousands of rows that you want it applied to.
formula in your case for C2 will be: =AND(LEN($A2)>0;LEN(C2)=0) this can be applied for C2:F2000 :
Or what you can do is specify this formula for C1 (=AND(LEN($A1)>0;LEN(C1)=0) ) and apply it to C:F.
No need for VBA.

How to determine current row?

I am using Conditional Formatting to try and change the colour of a cell in column A if the text in Column B is certain values. Currently my three formula's are this:
=$B$2="Low"
=$B$2="Good"
=$B$2="High"
I need a way to quickly push this to all 50 columns therefore I thought if I could change the formula to the following it would work I just don't know how:
=$B$ThisCellsRow()="Low"
=$B$ThisCellsRow()="Good"
=$B$ThisCellsRow()="High"
You're using absolute cell references (the $ in the address), which prevents Excel from adjusting the address as you add/remove rows and move the formula around. If you use $B2 instead, as you drag down the formula, Excel can adjust it to be $B3, $B4, $B5 and so on. With this format, the B column remains constant, and the row portion is flexible.

Resources