transcript

excelisfun - Excel Magic Trick 1383: Conditional Format Row w OR Logical Test with Multiple Partial Text Criteria

Thumbnail of the video “excelisfun - Excel Magic Trick 1383: Conditional Format Row w OR Logical Test with Multiple Partial Text Criteria”
Download Markdown
Community upload. AI output has not been independently verified.

Welcome to Excel Magic Trick number 1,383. If you want to download this Excel workbook so you can follow along, click on the link below the video. We've got a great video here about conditionally formatting a row with an OR logical test that has multiple partial text, or "contains," criteria.

In our last video, Excel Magic Trick 1382, we extracted records with the same logical test, and someone in the comments asked how to do the same thing but conditionally format the whole row. Here, row number one has a job title, and we need to search through that job title. If we find any of these text items, or "contains" criteria, within that job title, the entire row needs formatting, such as the color yellow. When we get down to this row, since the job title does not contain any of these text items, it should not get conditional formatting.

The trick to conditional formatting is that every single cell has to have a logical test to determine whether the formatting is applied or not. Notice that if we're looking at the phone number, date, or last name, that's a cell different from the cell that actually has the job title. So when we write our formula, every one of these cells is going to look at that job title. When we copy the formula down, a similar thing happens here: every cell has to look at "Senior Account."

I would like to be able to highlight the whole data set all the way down, go up to Conditional Formatting, and pick a built-in feature to do this, but there is no built-in feature. Anytime you want to conditionally format a data set, as long as you can think up the logical test, it can be done, but we have to use New Rule and build our own logical formula that gives every single cell a TRUE or FALSE.

Anytime I get into this situation, instead of highlighting and going up to the dialog box and building the formula there, I like to build it over in cells off to the side, copy the logical formula over and down, and see if the patterns of TRUEs and FALSEs work in our spreadsheet. Visually checking the TRUEs and FALSEs, once it works, I copy that formula, highlight the range, and paste it into the dialog box.

Here's the logic. We need to check this one against all these items, and this one against all these items. So I have a parallel range over here. In the top left cell, I'm going to use equals the SEARCH function. SEARCH looks for a particular text item within a larger text item and returns the position. If SEARCH is looking for "data," it counts until it reaches the 12th position and reports that it found "data" in the 12th position. Normally we put one item into find_text, but here we put a bunch of items into find_text. Then I hit the F4 key because I'm copying it. Since we put seven items into the find_text argument, that instructs SEARCH to return seven answers. This is an array operation, which means we're forcing the function to return multiple answers. Then the within_text argument is the text we're looking in for this row, cell F8. There is a relative cell reference to F8, which is the formula that will be copied in memory and copied over and down. Since the entire row needs to look at F8, I hit the F4 key one, two, three times to lock the F, so as I copy the formula to the side, F cannot move. When I copy down, it goes from row 8 to row 9, since there's no dollar sign in front of the 8. I close the parentheses, and for this cell I hit F9 to evaluate it. There's my array of positions and errors. Anytime this array gets a position, we know that record should get conditional formatting.

From this array, I want to get TRUEs where there are numbers and FALSEs where there are errors. So after the equal sign, I use the ISNUMBER function. It returns TRUE when there's a number and FALSE for anything else. I close the parentheses and press F9 to evaluate. There are the TRUEs, because it found two of these items in the first job title.

This is an OR logical test. If I get one or more TRUEs, I need to deliver a TRUE to the cell. The perfect function for that is the OR function. I close the parentheses and press F9. That's what I need: a single TRUE in the cell.

Before we enter this formula, it's an array formula, because we performed a function argument array operation in the find_text argument, inside the value, and even inside the OR logical argument. You always have to ask whether the argument holding the array operation can perform this without the special keystroke Control+Shift+Enter. The answer for these is no. Only five functions, LOOKUP, INDEX, AGGREGATE, SUMPRODUCT, and the CHISQ.TEST function, don't need the special keystroke. So, ready? Control+Shift+Enter. Immediately we go up to the formula bar and verify that the automatically entered curly brackets are in our formula. That's Excel telling you it evaluated it as an array formula. Now we can copy it over and down and look for our patterns of TRUEs and FALSEs: record number one, record number four, and so on.

I also like to go to the diagonally furthest cell away, click in the last cell, and press F2 to verify that all the cell references look right. Then I press Escape, return to the upper left cell with our formulas, press F2, and copy the formula in edit mode with Control+C, then Escape.

Now I need to highlight the entire data set. I highlight the first row, then Control+Shift+Down Arrow to highlight all the way down, then Control+Backspace to jump back to the active cell. It's important that this is the active cell, because we copied the formula from the upper left corner, and that formula was built from the point of view of that cell. Now I can go to Home, Conditional Formatting, New Rule, or use the keyboard shortcut Alt, H, L, N.

The option we want is "Use a formula to determine which cells to format." Notice the top item is highlighted, so to jump to the bottom I either click or use Page Down. Now we paste our formula here, to format values where this formula is TRUE. I can click or press Tab, then Control+V, and there is our formula.

Wait, how do we enter this with Control+Shift+Enter? Well, the Conditional Formatting dialog box understands it without any special keystroke. It takes that formula and treats it as if it's in that cell. It copies it over and down, recognizing the patterns of TRUEs and FALSEs, and formats the cells. I can choose the format from any of these four options. I'll simply choose yellow, click OK, click OK, and look at that.

This is dynamic. If I type "PowerBI" here and press Enter, the formatting should go away, because "analytics" is no longer in the list. Control+Z. But what if I want to add it to the list? Well, I can come down here, type "analyst," and press Enter, and lo and behold, that row does not get the formatting. The formulas we built don't have a dynamic range in them. Control+Z. If we convert this to an Excel table, Excel table column ranges are dynamic. Even though we built our formula looking only at the range J8 to J14, as soon as we convert this to an Excel table, the formulas will recognize it as a column in an Excel table, and when you add something below, the formulas will update.

Here's what we do. We click in a single cell and make sure this is a proper data set: field names in the top row, records in rows, and empty cells all the way around. I go to Insert, Table, or use the keyboard shortcut Control+T, and press Enter. Even though we don't need to worry about the name, I never like to have an Excel table without giving it a good name. Under Table Tools, Design, Properties, I set the table name to "ContainsCriteria" and press Enter. Now when I type anything below the Excel table, it automatically expands to include it. Hopefully I spelled it right. I type "analyst" and press Enter, and there it is. We can see that the table expanded, and look at that.

In this first example, we used this formula right here. I want to show you an even easier formula and learn something important about conditional formatting. Let's go over to the sheet 1383. We're still going to start our formula with SEARCH. We're going to try to find any of these items within the job title for this row. So we press F4 to lock the column but not the row, then close the parentheses. When I press F9, there is our array of numbers and error values.

Instead of using ISNUMBER and OR, I'm going to look at this as a lookup range and look up whether there are any numbers. Because there can be multiple numbers and we always want the last one, I'm going to use the LOOKUP function with approximate match. Control+Z. The LOOKUP function is the first lookup function ever. LOOKUP came before VLOOKUP and HLOOKUP. This function only does approximate match lookup.

How do I always get the last number with approximate match lookup? I give the lookup value a really big number that will be bigger than anything in the resulting array. I use the big number 2^15. Why 2^15? Well, 2^15 minus 1 is the maximum number of characters allowed in any given cell. By using 2^15, it's guaranteed to be bigger than anything SEARCH can return. Because we're doing approximate match, it always finds the last number. I close the parentheses and press Control+Enter, copy it to the side, and when we copy it down, we get something unexpected. Look at that: for every row that has numbers that are not zeros, we need the conditional formatting. But what about these errors?

The useful thing about the Conditional Formatting dialog box is that it interprets any nonzero number as TRUE and interprets the errors as FALSE. That's all we need. I come to the top left corner, press F2, copy this in edit mode with Control+C, then Escape. I highlight the first row with Control+Shift+Right Arrow and Control+Shift+Down Arrow, then Control+Backspace to return to the active cell. Then Alt, H, L, N, Page Down, Tab, Control+V. I click Format, choose a color, click OK, and click OK. There we are: each row is conditionally formatted with a formula that was easier to create. We didn't have to use Control+Shift+Enter to enter it in the cell, and an approximate match LOOKUP formula like this calculates much more quickly than our SEARCH and ISNUMBER formula.

In this video, we saw how to conditionally format rows based on multiple "contains" or partial text criteria in an OR logical test. We saw how to do it with a LOOKUP and SEARCH formula. Back on the earlier sheet, we saw how to do it with OR, ISNUMBER, and SEARCH. We also saw how to create an Excel table so that we can expand or contract our criteria. That's a lot of fun with conditional formatting and logical formulas. We'll see you in the next video.