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.
Related
I would like to highlight a cell which is the closest the a specific value but still lower (below) or equal according to a formula.
For instance, if I have 14 in B4, I would like the cell 13 (or 14) highlighted but not 15 in the range.
The value I need to look for would be in B4 and the range that I must apply the highlight formula to would be L1:L371.
So far, the best result I was able to achieve was with (from what I was able to find using Google or Stackoverflow already existing content):
=SMALL($L1:$L317,COUNTIF($L1:$L371,"<="&$B$4))
Although, not only it selects the value I am looking for, it also selects all the values below.
May someone provide me some help so I can achieve the expected result please?
Thank you for your time and help, it is greatly appreciated.
Based on your description, it seems like you need to find the maximum value of a range (L1:L137) that is less than or equal to an input variable (B4) and highlight that value in the search range. Depending on what version of Excel you have, here's what you need to do:
For Excel 2019 or Excel 365
You can use the MAXIFS function. You would actually put that function in a conditional formatting rule, but more on that in just a second. As a test, put the following code in cell B5:
=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
The first argument is your "MAX_RANGE", or the range that contains the values you want to find the max of. It is also our "CRITERIA_RANGE", the second argument. The third argument is the criteria itself, which is that the search range must be less than or equal to the value in cell B4. Essentially, we are going through the list and creating a subset of numbers that are less than or equal to our search value, and discarding the rest. I assume you are aware of relative vs. absolute referencing, since you used the "$" anchor in your referenced code. If not, here's a description of the difference.
Now, the result of that formula should be the highest number in the list less than or equal to the search value, but that doesn't highlight it for us in our list. To do this we need conditional formatting. To do this, highlight your data range (L1:L137) and go to the conditional formatting drop down on the home tab of the menu ribbon. Choose "New Rule". On the dialog that pops up, choose, "Use a formula to determine which cells to format". In the formula input box, enter:
=L1=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
This will compare whether the value in the cell being evaluated (L1, for instance) is equal to the result of that formula we talked about above. Since L1 is only relatively referenced, this formula will work for every cell in the data range.
Now, before you hit "OK" on the dialog, select the Format button. This will allow you to adjust your highlighting and formatting as you desire. Click "OK" on the formatting dialog, then "OK" on the Conditional Formatting Rule dialog. This should now highlight any data cell that is equal specifically to the result of our formula, and not everything that's less than our value.
For Earlier Versions of Excel
The concept is the same in earlier versions of Excel, but unfortunately, the MAXIFS function is not present in these versions. Instead, we must use an array formula. Array formulas are a whole other can of worms, but ExcelJet is an excellent resource. In fact, they talk about this very issue, here.
Unfortunately, we can't put the array formula in the conditional formatting formula like we did above, so we'll need to put this formula on a cell in the worksheet, then the conditional formula should reference that new cell. So in cell B5, if you put:
=MAX(IF(L1:L137=B4,L1:L137)
And then, instead of pressing Enter, you must press Ctl + Shift + Enter
This keyboard combination will tell Excel that you are trying to enter an array formula. If you don't press these keys, then the formula will error. Once you have entered the array formula, if you put your cursor in cell B5, you will see the formula bar at the top has added curly braces ({ , }) around the formula to look like
{=MAX(IF(L1:L137=B4,L1:L137)}
This leads to the same result as above, but is just achieved slightly differently. Now, following the same process described above for conditional formatting, you will simply set the formula to:
=L1=$B$5
And that should be it! Hope this helps!
I tried selecting the entire range (e.g. A2 to Z400), then put in the condition for instance: =B2<A2 to hope that a "ranged" conditional format would apply, but it doesn't seem to be the case.
I can probably come up with a VBA solution, but I was hoping to have an built-in Excel method if possible
Utilize conditional formating is indeed possible.
Select the desired range.
Select Conditional Formatting.
New Rule..
Use a forumla to determine which cells to format.
For the formula, enter
=IF(B1<A1;1;0)
Also, choose how you which to format.
This checks wheter B1 is smaller than A1. If true, it would return 1 which is boolean for true. This logical test should solve your issue. See screenshot below for the rule applied to range A1:B5.
Rule applied to range A1:B5 EXAMPLE
I'm currently working in excel, and I'm trying to figure out a way to find if multiple cells contain the string value of another cell, and if it does highlight the cell where the row and column meet up. I created an example of what I want, only it will be on a much larger scale.
I've tried using: =ISNUMBER(SEARCH(substring,text)) but I'm not quite sure how to use it the way I want to.
Any help will be appreciated!
Your approach is correct, we can use the fact that conditional formatting is applied like dragging a formula, adapting relative references.
Create a conditional formatting formula rule:
=ISNUMBER(SEARCH(B$1,$A2))
Applied to B2:D7
Your formula will work nicely; what you'll want to do is put that formula into all the cells you want to highlight, so you get FALSE and TRUE in every cell.
You'll then use two Conditional Formatting rules. The first will look for Cell Value = TRUE, and will set cell background and font colour to yellow. The second will look for Cell Value = FALSE, and will set cell background to No Colour and Font to White.
This will reproduce the result you're looking for.
Edited to add:
It is possible to do this using just Conditional Formatting too, but it's a little more fiddly. If you want to try it, you can do this:
Highlight your range, and take note of which cell is Active - that's the cell within your highlighted range that is still white. It's also the one whose address is shown in the Name box in the upper left. For the sake of this answer, we'll assume that's B2
Create a new Conditional Formatting rule. Choose "Use a formula to determine which cells to format".
Use the formula =ISNUMBER(SEARCH(B$1,$A2). Set the format to colour just the cell background.
Note where the $ appears in the formula above - you want to leave the row number anchored in the first part, and the column letter anchored in the second part.
This takes advantage of the fact that Conditional Formatting is able to use absolute, relative, and mixed references to find which cells to format. It's also a tidier solution, but it can be harder to maintain if the sheet is ever repurposed or modified.
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.
What I'm doing is a simple map on one tab of an office space that has all the cubicals laid out. On this map, I have conditional formatting checking another table of listen computers for the last time they were updated/maintained. It works as it stands, but we are moving into a new building and the map (which is already done) has WAY more cubes than last time. So I'm trying to figure out a more efficient way to do this task.
Here is the code:
=AND(VLOOKUP("CCA C1",LOCATION,3,FALSE)<>"",VLOOKUP("CCA C1",LOCATION,3,FALSE)<TODAY()-80)
So as it sits, it works fine. But what I'm trying to do is change the "CCA C1" to simply read the contents of the cell it's formatting so I can just format an entire range of cells rather than doing it cell by cell. The "CCA C1" is in the location of the second sheet (it's a named range). So this checks that entire range for "CCA C1" and checks if the date a couple columns over in that row is within 80 days, if it's not it highlights the cell in the map in red reminding us we need to check the computer. But what I would like to do is simply put "CCA C1" in the cell in the map (which is already is labeled), and have this check for the value of the cell it's formatting and look for that value the same way it's looking now. Just without me having to put "CCA C1" in the formula, I'd like to say something like this:
=AND(VLOOKUP(CURRENT CELL VALUE,LOCATION,3,FALSE)<>"",VLOOKUP(CURRENT CELL VALUE,LOCATION,3,FALSE)<TODAY()-80)
Make sense? Anyone know of a way to do this? Otherwise I have to conditionally format each individual cell with the value manually rather than just format all the cells with the same formatting and have the formula check the contents of the cell for what to look for in the location range of the other sheet.
And to clarify, I know that I can put in the actual cell, such as E3, but then I still have to manually change the formula for each cell which defeats the purpose. I want to just say current cell or something like that. I have 3 conditional formats for each cell, I have around 100 cells to be formatted, so rather than having 300 formats I have to put in, I'd love to just do 3. Not sure if this is possible, that's why I'm asking :)
Just replace "CCA C1" with the address of the first cell in the range of cells with the conditional formatting. Assuming your conditional formatting starts in B2:
=AND(VLOOKUP(B2,LOCATION,3,FALSE)<>"",VLOOKUP("CCA C1",LOCATION,3,FALSE)<TODAY()-80)
EDIT: As I commented, I'm not sure I understand the issue, but if I do, you need to enter the range of cells with CF in the applies to range of the CF dialog, rather than copying and using Paste Special:
Note that this works with the merged cells.
You will need to adjust the applies to range as you add more computers, etc., but the same formula will work.