Welcome to Excel Magic Trick 1,242. If you want to download this Excel workbook and follow along, click on the link below the video. We have this huge data set, around 3,000 plus records, with all sorts of different countries and all sorts of different measurements. For Afghanistan, for example, we have many different measurements, and the data runs from 1970 all the way to 2013. That's a lot of data, but we only want certain countries. Those countries are listed in a single cell, separated by commas. We only want the years 2000 to 2010, and we only want the gross domestic product number for each of the desired countries.
If you go over to the sheet notes, Mr Excel and I did Dual Number 174, where we looked at Advanced Filter and Power Query for solving this exact problem. In this trick, I want to show you Text to Columns, the MATCH function, and a straight filter.
The first thing is that if I filtered this directly, I could put a filter on and check each individual item, but there are too many countries for that. So I'm going to break this comma-separated list out into separate cells. I'll use Data, then Text to Columns, or the keyboard shortcut Alt, A, E, and choose delimited by a comma. When I select comma, I can already see there's some trouble: there's a space. I could select space as a delimiter, but that won't work because some of the countries out here have a space in their names, and it would break those two words apart. So I'm actually going to do this in two steps, selecting comma, and when I click Finish, instantly I have the list broken out into cells.
Next, I'm going to get rid of the extra spaces using my favorite haircut function, TRIM. When I press Control Enter and copy this over, TRIM removes all spaces except for single spaces between words, which is exactly what we want. I'm going to insert one, two, three columns because I eventually want to extract the data and put it below this data set. I'll copy the TRIM formulas, then move down to the bottom of the data set with Control Down Arrow, and somewhere off to the side I'll paste. Rather than a regular paste, I'll go to the Home tab and open Paste Special, or use Control Alt V. I want to transpose the list, because it was horizontal and I need it vertical, and I also want to paste Values Only. Click OK, and there's my list. I'll type "Countries" in bold, then Control Home to jump to cell A1.
Now I need to get the countries, GDP, and the right year. I'll start by adding a separate column labeled Countries, and then I'll build a formula using the MATCH function. MATCH can look something up. I want to look up each country name in the lookup array, which is the list of countries I want. I'll click into that cell, use Control Down Arrow and Control Shift Down Arrow to select the list, and press F4 to lock the reference. Then I add a 0 for an exact match and close the parentheses. Press Control Enter, then double-click to fill the formula down. This gives me the relative position of each country in the list. Argentina, for example, is the first item.
Next, I want to see TRUE when there's an actual number, so I wrap the formula in ISNUMBER, which returns TRUE when there's a number and FALSE when there isn't. I can fill this edited formula all the way down with Control Enter.
Now I'll turn on the filter feature with Control Shift L. The filter will work on this column and the next, but the other columns need a different approach. I'll start with the Gross Domestic Product field, unselect all, and select only Gross Domestic Product, then click OK. I'll scoot the filter off to the side so I can see it, then select only TRUE and click OK. The data set is almost there. I can use Control Down Arrow to see the few remaining records, which is exactly the number of countries we want.
Next, I'll highlight the columns I don't need, everything all the way over to 1999, right-click, and delete them. I also don't want the last three columns, so I'll highlight Q through S, right-click, and delete. Almost done. I'll press Control Asterisk to select everything, then Control C to copy just the visible records.
Now I'll insert a new sheet, click cell A1, and paste with Control V. I'll use the Paste Options smart tag to keep the source column widths. I don't need columns C and A, so I'll select C, hold Control, select A, right-click, and delete. I'll zoom out a bit with Control and the scroll wheel.
I'd like the numbers to display in millions. I'll highlight the numbers and press Control 1 to open Format Cells, then go to Custom. The number format shows the full numbers with commas, but if I add a comma at the end of the format, the display behaves as if I divided by 1,000. Adding another comma makes it behave as if I divided by a million. The underlying values remain in the cells; they are just displayed in millions. When I click OK, the sheet looks much better.
Next, I'll highlight the year field names at the top. These are stored as numbers, so I can use custom number formatting to indicate that they are in millions. I'll go back to Control 1 and Custom, then enter a 0 in double quotes, a space, an M, and close the double quotes. That indicates the values are in millions. I'll click OK.
Now I'll right-click and insert two rows above the table. Then I'll go back to the original sheet, press Control Home because this is an important label, and copy the title: "GDP breakdown at constant 2005 prices in US dollars, all countries." Then I'll paste it into the new sheet. This title tells the reader that the figures are in constant 2005 US dollars and that the values are in millions.
A few more formatting touches remain. I'll add a border right below the field names by opening Format Cells on Borders and setting the bottom border. I'd also like to alternate white and blue columns. I'll highlight the first cell, go to the fill bucket, and choose a light blue. Then I'll fill the next cell with white. Since I want the formatting copied, I'll use the Format Painter. I'll highlight both cells, double-click the Format Painter on the mini toolbar so it stays active, and click through the cells to apply the alternating colors. If I click the wrong cell, the painter misapplies the format, so I need to be careful and press Escape to turn it off when I'm finished. I'll highlight the extra cells I applied by mistake and remove formats and contents.
A few housekeeping items remain. The first column should be left-aligned, and the numbers should align to the right. If I press Control P to preview the printout, it spreads messily across many pages. I'll go to Page Layout and open the page setup dialog, then choose Landscape and Fit To One Page. I might need to come back and adjust this later. I can also set the horizontal margins and add a header or footer. I'll create a custom footer and put the sheet name in the center. If I change the sheet name, the footer will update automatically. Then I'll click OK and check the print preview. The preview may not be fully accurate, but the printout should look good.
We went from a huge data set to a clean final report using Text to Columns, TRIM, MATCH, filtering, and some formatting, and then we dumped the results on a new sheet. We'll see you next trick.
