Welcome to Excel Magic Trick number 1473. Hey, in this video we want to see a formula that will create a sequential number incrementer for just the middle number. Now, here's the pattern we need to create with a formula: we need a formula in this cell, copy it down, and it remains "AA-" at the front and "-17" at the end, but the middle part needs to increment as we copy the formula down.
Now let's go over to the sheet 1473. In cell A4 I have just some text, and actually that's going to be the start of our formula. Right before this text string, I'm going to type an equal sign, then double quotes, and then "AA-" in double quotes. Anytime we have text in a formula, you have to enclose it in double quotes. We're going to come back and add a formula element to the middle of this, so the text won't change but the middle part will. For the time being, I'm going to Ctrl+Enter and then copy this down. That's just going to show us the same text string all the way down, but we'll come back and change it.
I want to come over here and look at the formula element that we use when we want, as we copy the formula down, to have the numbers 1, 2, 3, 4, and so on. Equals, and we use the ROWS function. The ROWS function just counts how many rows there are in a range. So from 4 to 8, that is 5 rows. If I close parentheses and Ctrl+Enter, ROWS reports 5. But I want it to change as I copy down. So I press F2, and right inside of the ROWS function we're going to look and see which cell we're sitting in. We're in C4, so I'm going to type C, dollar sign, 4 to lock the row reference for the first cell reference, colon, C, no dollar sign on the second cell reference. That's the end of the range. That means as I copy down, that $4 is always locked, but that one's free to move to 5, 6, 7, 8, and so on. Right now ROWS will report 1, because how many rows are there from 4 to 4? Ctrl+Enter. But when I copy this down, not only will it give us our incrementing numbers, but inside the formula, if I hit F2, notice 5. Enter, F2, 6. This is called an expandable range. We lock the first row reference but not the second. Enter, F2, it is expanding as we copy the formula down. Of course ROWS is always counting one more each time.
Now I want to come over to the top cell and hit F2. We're going to have to create that formula element right here. If I just type the formula element, it would not work, because really I need some text, then I need to join it to the formula element, and then I need some text at the end of the formula. Watch this cool trick: double quote, ampersand, ampersand, double quote. The last bit is its own text, the first bit is its own text. Right in between, I need to create my formula element. So now I type ROWS. I'm sitting in A4, so a dollar sign for the row, $4, close parentheses. There it is, right in between the two ampersands and the two text strings. That is my number incrementer. Ctrl+Enter, double-click, and send it down. Well, at least the number incrementer part is working correctly, but I need to add leading zeros before we add in our formula.
The leading zeros: let's see how to do this with number formatting. I'm going to highlight the whole column. Ctrl+1 is the keyboard shortcut to open up Format Cells. I go to Number, down to Custom, and in the Type argument I want to create my own custom number formatting: zero. Notice the sample. If I type a second zero, that adds a leading zero. Another zero adds still yet another leading zero. So I simply type four zeros, and that will display significant and insignificant digits, always adding the proper leading zeros. When I click OK, that is exactly what I want.
The thing is, with formulas, if I come over here and try to highlight all of this with Ctrl+1 and custom General, it will not work. This is number formatting, and we have text in this. Click OK, it doesn't matter, it's not going to work. So I'm going to Ctrl+Z to get rid of that. But luckily, if I come to the top cell and F2, since that's a number, I can use the TEXT function. The TEXT function specifically takes a number, and if you give it the right custom number formatting inside the formula, it'll take the number, format it, and convert it to text. So: comma, in double quotes, 0000, close double quotes, close parentheses. Now TEXT allows us, inside our formula, to apply a custom number formatting and convert the number to text. Ctrl+Enter, double-click, and send it down. There we go. If I go to any cell, I see the expandable range with ROWS giving my number incrementer, and then the TEXT function to add custom number formatting.
Now, an important point about formulas like this: we use ROWS here, but oftentimes people use the ROW function without an S. This will get you in big trouble. What they do is say, OK, cell A1. That's much easier to create than having to remember dollar signs and all that. Ctrl+Enter, double-click, and send it out, no problem, we can add the custom number format at all. But here's the problem: if we change the structure of the spreadsheet, like we insert a row, then because it was pointing to a cell that has been pushed down, it reports the incorrect number. That's why when you're doing number incrementers, you always use ROWS and you refer to the cell itself where the formula is sitting, and then you don't run into that problem.
Alright, that was a lot of fun with ROWS to increment numbers and TEXT to add number formatting inside our formula. If you liked that video, be sure to click that thumbs up, leave a comment, and subscribe, because there's always lots more videos to come from Excel is Fun. All right, we'll see you in the next video.
