Welcome to Excel Magic Trick number 1,143. If you want to download this workbook and follow along, click on the link below the video. In this video, we have a list of items with some empty cells, and our goal is to keep the original list, including the empty cells, but extract all of the content that sits in non-empty cells.
If this needs to be an automatic process, so that if the data changes the extraction happens automatically, then you need to use a formula. The formula for data extraction is an array formula, which is pretty complicated. But if it's just a one-time deal, check this out. We can highlight the data and use Control+Shift+L to add an AutoFilter. Click the AutoFilter and just check Blanks. Instantly, the original list is kept intact, but we see only the cells that have content. Press Control+C to copy, and check that out: only the visible cells have the dancing ants. I can come over to the side, because there are some hidden rows. I'm going to come below the data set over here and press Control+V to paste just the cells that have content. Now I'm going to come back over here and press Control+Shift+L to turn the filter off. I love that. That is amazing.
If it needs to be an automatic process, then we're going to have to build an array formula. In Excel 2007 or earlier, we have to use the INDEX function and the SMALL function. If you're in Excel 2010 or later, you can use AGGREGATE as a substitute for SMALL, and you won't have to use the special keystroke Control+Shift+Enter to put your array formula in the cell.
Let's look at what we really need. If we're going to use INDEX over here to look up each one of these items, we need the relative position. So we need 1, 2, 4, 5, 6, 9, 10. How are we going to do that? As we copy down, we need those relative positions. We're going to use AGGREGATE, which will give us access to the SMALL function. AGGREGATE is an amazing function that came in Excel 2010. Functions 1 to 13 are for the second screen tip option. Notice the first one has that array and the K. If I click up here, I can see that functions 1 to 13 do not do array calculations. Only 14 to 19 allow array calculations.
So I'm going to use SMALL with 15, comma. Our array operation will have errors, and we need to ignore them, so we use 6, comma. This array right here is the magic of this function: any array calculation or formula element we put here, we can simply hit Enter, and the formula will know to calculate correctly. Now we need relative positions, so we use this well-known formula element, ROW, to get the row. ROW gives us 2 to 11, which is not what we need. Press F4 and close parentheses. We have to subtract the row of 2, so we press F4 and subtract two. For the first item, minus two gives us zero, so we then add one back in. Close parentheses, click on the array, and press F9. That is awesome: an array of all the relative positions for this list.
What's so cool about this formula element is that if I insert rows above or move things around, it's robust. It will always give me 1 to 10, and if I insert new rows, it will work perfectly. But that's not what we really want, because we don't want 1 to 10; we want only some of them. So we divide by an open parenthesis. I'm going to highlight the whole range F4 and use the comparison operator "not equal to" with the double quotes, which is our syntax for nothing, or an empty cell. Technically, that is a zero-length text string. If we had a zero-length text string in any of these cells that look empty, it would also pick that up. In essence, this part is asking whether the cells are empty. Press F9, and it gives me FALSE when it sees an empty cell. Press Control+Z. This calculation divided by TRUE will give us the actual relative position, and divided by FALSE, which is like dividing by zero, gives us a divide-by-zero error. Highlight and press F9, and we can see we've filtered out the relative positions we don't want, with the divide-by-zero errors. The six will ignore those. Press Control+Z.
Notice that in the first cell we need 1, in the next cell we need 2, and as we copy down, the next cell needs 4. What do we need? The K. This is the formula element that will increment the numbers 1, 2, 3, 4 as we copy the formula down. We use ROWS, and I'm sitting in D2, so I write D$2:D2. Notice one of the 2s is locked, so as we copy down, that 2 won't change, but the other one is free to move to 3, 4, 5. ROWS says how many rows there are: from 2 to 2 is one; when it gets to 2 to 3, that's two. Press Control+Enter. I didn't use Control+Shift+Enter, as you do for array formulas. Copy it down, and those are our relative positions: 1, 2, 4, 5, 6, 9, 10.
Now I'm going to F2, where we use that inside the INDEX function. The array holds the items to look up, so I highlight F4 and add a comma. Then comes the row number argument, which really means the relative position in the list. Close parentheses, press Control+Enter, double-click, and send it down.
Now we need to deal with that #N/A error. I'm going to come up to F2 and click on the K immediately, because I'm going to use that formula element again. I'm going to write IF with a logical test, which I need as TRUE or FALSE. Here, if the row number (the increment 1, 2, 3, 4, 5) is greater than the COUNTA, which counts non-empty cells, of F4, close parentheses. When we get past seven items, it will return TRUE. Then the value if TRUE is double quotes, a zero-length text string, which shows nothing. Otherwise, the value if FALSE is the INDEX formula. Run the array formula. This is also good for performance: on giant data sets, that logical test means that once the row count gets past the number of items, the formula just returns a null text string and never runs the whole array formula. Close parentheses, press Control+Enter, double-click, and send it down. Now if I add something here, the list updates instantly.
What about earlier versions? We can almost use this same formula. I'm going to cheat, escape, and copy and paste right here, but we have to get rid of AGGREGATE. This whole thing needs to change. I'm going to delete it in just a second, but I don't want to retype all of this. I'll highlight some of the formula elements and press Control+C to open the Clipboard, because I'm going to load these formula elements up. I'm highlighting, pressing Control+C, and loading them over here, along with this too. Notice the rows: we didn't lock the C when we copied it over, so it sits in the right cell reference. Control+C. Now I'm going to go to the top, to the row number, and delete it. Now I have to use SMALL instead, and in the array I'm still going to need that array.
I'm going to write IF, and the logical test is an array calculation. Here's our logical test. We don't need those extra parentheses, because it gives us an array of TRUEs and FALSEs. I come to the end and write the value if TRUE, which is the row number. For the value if FALSE, I leave it out. By leaving it out, FALSEs will be inserted into the array where we saw divide-by-zero errors from AGGREGATE. Close the parenthesis. Click this and press F9, and there's the filtered list again, except that the divide-by-zero has now become FALSE. Press Control+Z. Now there's the K. We come to the end of the IF, paste our formula element for incrementing rows, and go to the end. I'm lost in the formula, so I'll use the screen tips. When I close parentheses, it tells me I'm in the row number, which is the whole SMALL. I come to the end, close parentheses, and now I'm at the value-if-FALSE, which is the whole INDEX. Close the parenthesis, and the black parenthesis shows.
If I hit Enter and copy it down, implicit intersection gives a value there, but we're going to get errors all the way down. So I'll go to F2 and put it in edit mode. Control+Shift+Enter tells Excel to calculate it as an array formula. Press Control+Shift+Enter, and scroll this down. The curly brackets show that Excel understood it's an array formula. Now when I double-click and send it down, it works like a charm. If I put something here, it instantly works, and this works with numbers or text. Control+Z.
So: for Excel 2007 or earlier, or Excel 2010 or later, and by all means, use the filter if it's a one-time deal. We'll see you in the next video.
