Welcome to Excel Magic Trick number 1,373. If you want to download this Excel workbook, Excel Magic Trick 1373, so you can follow along, click on the link below the video. We have a great video here: we need to see how to extract every fifth value and conditionally format every fifth value. This is the answer sheet, and watch what happens when I change this five to a six. I haven't touched the conditional formatting. Watch the extracted values: when I hit Enter, instantly every sixth value is conditionally formatted in the original data set, and here we have 128, 341, and so on.
All right, let's go over to the sheet 1373. Here are our numbers. The very first thing we're going to do is count how many values we have over here that sit in every fifth row. I'm going to do this two different ways. The first way is to count how many rows there are. I'm going to click in the top cell, press Control Shift Down Arrow, then Control Backspace to jump back to the active cell. Rows is a simple function: it counts from 2 to 94 and sees how many rows there are. Then I'm going to divide it by five. When I hit Enter, if there were exactly a multiple of five it would give me a whole number, but it gives me a fraction, and I want to round this down. So I'm going to use the ROUNDDOWN function. There is the number, and I'm always going to round down to the integer, so for number of digits I put a zero, close parentheses, and Enter.
If I have a column that actually lists the number of records, then I can simply use the MAX function. Clicking the top cell, Control Shift Down Arrow, Control Backspace, close parentheses, and I'm going to divide by this five. It gives me the same 18.6, but now, instead of ROUNDDOWN, I use the INT function, since we're always going to have positive numbers. INT always takes the integer moving down, so it will go from 18.6 to 18.
The next thing I want to do is conditionally format. I need every fifth row highlighted in yellow, and so on. We're going to highlight this whole range, then go to Home, Conditional Formatting, and look for an option to highlight every fifth row. It's not there, so no problem: we can use New Rule and use a logical TRUE/FALSE formula. If I'm going to build a formula and put it in the dialog box, in memory the dialog box will copy it through every single cell, and every single cell in this data set will have some logical test. Am I in the fifth row? False. Am I in the fifth row? False. When it gets down here and I ask the question "am I in the fifth row?", that will yield TRUE. So the conditional formatting will be a pattern of TRUE and FALSE.
Anytime you build a formula to enact conditional formatting, I like to build it in the cells off to the side, copy it over and down, and see if the patterns of TRUEs and FALSEs work. For both the formula for conditional formatting and the formula for extracting every fifth record, we're going to have a formula element called a number increment. Since we're copying down across the rows, I'm going to start with the ROWS function. I need ROWS to give me 1, 2, 3, 4 as I copy down. I'm sitting in J2, so I type J$2 and close the parenthesis. Lots of people post questions asking why I don't just use the ROW function. The ROW function, if you were to insert a new row somewhere, would give you a potentially wrong answer, but ROWS will never give you the wrong answer. I also cover this topic in my book. Control Shift Enter is a robust method of generating the numbers 1, 2, and so on inside a formula. Notice that if I copy it down and then over, it gives me 1, 1, 2, 2, which is exactly the start of my formula.
Now watch this. I've highlighted the entire range, and the active cell is at the top. I'm going to put it in edit mode with the F2 key. The question I need to ask is "am I in row five? am I in row ten?" So I'm going to use the MOD function. The MOD function gives you the remainder after division. If I put the number increment, then a comma, and for the divisor I give it this five, I need to lock that in all directions, so I hit the F4 key. 1 divided by 5 has remainder 1, 4 divided by 5 has remainder 4, 5 divided by 5 has remainder 0, and that will be our trigger. To populate this formula through the highlighted range, I use Control Enter, and there we go: our trigger will be zero. F2 puts it in edit mode, then I ask the question "are you equal to zero?" and Control Enter populates it all the way down, and there are our patterns of TRUEs and FALSEs.
If I want to use this formula in the dialog box for conditional formatting, I come to the top-left cell, press F2, and copy this in edit mode with Control C, then Escape to revert back to whatever was in the cell before I put it in edit mode. Now we need to highlight the entire range: highlight the top two cells, then Control Shift Down Arrow. Watch this: I use Control Backspace to jump back to the active cell. That works whether you've highlighted a range or, as we saw over here, when you're in edit mode creating a formula. Notice I copied the formula from the upper left, so make sure the active cell is in the upper left. Then Home, Conditional Formatting, New Rule, or we can use the keyboard, Alt, H, L, N. There's a list of options here, and the very last one is the one we want: "Use a formula to determine which cells to format." I could click with the mouse, or because the top item in the list is selected, I can hit Page Down to essentially jump all the way to the bottom. Once we select this, we want the text box "Format values where this formula is true." I can click with the I-beam or hit the Tab key, then Control V to paste the formula that was copied in memory, which will apply over and down, getting a TRUE or FALSE in every cell. Click Format, and you can apply whatever formatting from whichever of the four tabs you want. Click OK, click OK, and look at that. We can test this with six and press Enter. That is pretty amazing. Control Z.
Now we want to come over to our extracting formula. We're going to use the INDEX function and give the INDEX function the entire column here. INDEX just needs to know which row, or relative position in the data set, you want to extract, so we need 5, 10, 15 in our formula. We'll build a number increment. I'm going to use the ROWS function. I'm sitting in H2, so I type H$2:H2 and close the parentheses. We know that will give us 1, 2, 3, 4, 5, 6 as we copy down. So we simply multiply, using the left arrow key to get that 5, then F4 to lock it. Control Enter, and if I copy this down far enough to accommodate all the possible values I'm going to extract, there it is: our number increment, 5, 10, 15, 20.
Now I've highlighted the whole range with the active cell at the top, and I simply hit F2. After the equal sign, I type INDEX and press Tab. INDEX is a lookup function, and it has an array, which are the potential values you want to go get from somewhere and bring back over here. So I click in the top cell, press Control Shift Down Arrow, then F4 to lock the range, which jumps the screen back into view. I type a comma. We're using the top option, not the bottom option. I never use that bottom option because it involves multiple-table lookup, and we're not doing that. Plus there are other, better ways to do multiple-table lookup. Look at this: our argument is row number. Row number means the actual relative position. Notice that 184 is in row six, but we need relative position five. Even though it says row number, think of it as relative position. It's already in there. Come to the end, close the parenthesis, and populate this formula all the way down with Control Enter. Look at that: 184, minus 14, just what we have in our data set.
Our formula is not quite done, because you can see we have errors down here. I want to amend the formula at the top, copy it down, and in the cells where there are errors, display nothing. But I also want to build an efficient formula, so I don't have to run the formula in order to determine whether there is an error. In the active cell at the top, I hit F2. What I'm not going to do is use the IFERROR function. We could use IFERROR because this is a small data set, but I like to build efficient formulas and get in the habit of using one that will be efficient in any situation. If the only way to determine that there was an error were to run the formula, that's the situation where you'd use IFERROR. But any other time, if there's an alternative logical test to determine when to run the formula and when to display nothing, you should use that. For us, we know how many records to extract, and as we copy down, once we get past row 18 we just want to display nothing. We don't actually have to run the formula to get an error. So I'm going to use the IF function and the logical test. I'm going to cheat: I already have my number increment here, so I'll copy that in edit mode, very carefully. For the logical test, I type a comma, just so I don't confuse myself. The logical test is: when the number increment is greater than our count of 18. I lock that with the F4 key. Notice that comes out TRUE or FALSE down here: TRUE, TRUE, TRUE. Then a comma for the value if true. That's when we want to display nothing, and the way you do that in an Excel formula is with double quotes, double quotes. Technically that is a zero-length text string. Excel considers it text that has zero length, but that's what we use to display nothing. Then, if we're not past row 18, we'll run our formula. Come to the end, close the parenthesis, and populate this all the way down with Control Enter. There we go.
If I come over here and say, please show me every third value, instantly the conditional formatting updates. There are our numbers back over here. Every seven, Control Enter. That is amazing. Five, Control Enter.
All right, that was a little fun: conditionally formatting every fifth record, then looking at a formula for counting how many there are, and a formula to extract them all. We'll see you in the next video.
