transcript

excelisfun - Excel Magic Trick 1222: Formula To Add 10kg, 6kg, 19kg: Array Formula Or Custom Number Format?

Thumbnail of the video “excelisfun - Excel Magic Trick 1222: Formula To Add 10kg, 6kg, 19kg: Array Formula Or Custom Number Format?”
Download Markdown
Community upload. AI output has not been independently verified.

Welcome to Excel Magic Trick number 1,222. Hey, if you want to download this workbook, 1,221 to 1,223, and follow along, click on the link below the video.

Hey, we have a column of weights there. We go, and there we go. However, when I try to add up this column, I get zero, but when I try to add up this column, I get the correct total. What in the world is going on? Now let's scroll over here and let's just try this. Alt equals is the keyboard shortcut for AutoSum. I'm going to click in the top cell, Control Shift Down Arrow, Shift Enter to put that in the cell, and push the cursor up. Now, this is never going to work, because if you come over here and hit F2, someone has typed 44 and a kg. That means there's text and numbers, and some function is not going to do that. Not only that, but this is text, because it doesn't have the default alignment for numbers. Someone came up and actually, in the Home ribbon, added right alignment to make it look like a number.

Now if I come over here, Alt Equals. These are simply numbers. I click on the top cell, Control Shift Down Arrow, to highlight all the way to the bottom, and then Shift Enter to put the formula in the cell and push the cursor up. Now, here we're just adding numbers, but over here we have text, and we will see how to deal with this, because sometimes you do get this situation.

Over here we want to display these numbers with the kg, but allow them to remain numbers so that the SUM function can add them. So I'm going to click on the top cell, Control Shift Down Arrow, to highlight all the way to the bottom, Control 1 to open up Format Cells. Number formatting is what we want. We come all the way down to the bottom and click on Custom. Now, in the Type text box, we highlight, and we can see the default number formatting, General. Now I'm going to type a zero, and I always want it to display the text kg, so in double quotes, kg, in double quotes. You can immediately see the number formatting appear. That means any number we type into a cell in this column will show on the surface of the spreadsheet as kg, but the number will still be underneath. There's our preview. When I click OK, it works perfectly. The SUM function is looking at numbers, because those are numbers in the cell, and just the kg is displayed on the surface with custom number formatting.

Now, what in the world do you do over here? What if you really had to keep that, where this was a data dump or something like that, and you couldn't get rid of the kg? Well, we can do something pretty tricky here. We could actually add an extra helper column and teach ourselves how to use the SUBSTITUTE function. SUBSTITUTE can take something from a text string and remove it, or substitute something for it. So I'm going to say, hey, there's the text, comma, the old text I want to get rid of is double quote kg double quote, comma, and the new text I want is double quote, double quote. That is a zero-length text string, and for us, inside of SUBSTITUTE, it just means put nothing. Close parentheses.

Now watch this. When I Control Enter, it's immediately got the default alignment for text, because the F2 SUBSTITUTE function is spitting out text. We don't want that, because if I was to say equals SUM and point towards a text item, SUM is programmed to ignore text. We can convert any number stored as text back to a number by doing any math operation. I could do times one, divided by one, double negative, but I'm going to add zero. Notice it's a math operation that won't change the value of the number, so it's just the math operation to convert it back to a number. Control Enter, double click, and send it down.

Now notice, if I were to click in the top cell, F2, Enter, F2, Enter, I actually have to get the SUBSTITUTE to look at every single one of those numbers. Now, I don't want this extra helper C column. I actually want to do this over in the cell. So watch this. I'm still going to use the SUBSTITUTE function. The text, I'm going to highlight the whole column, Control Shift Down Arrow, Control Backspace to jump back to the active cell. Now, I've checked this out: that text argument is expecting a single text string. I just gave it a bunch. The fact that I gave it a bunch of items means we're doing an array operation. That means we're telling SUBSTITUTE to do the substitution on all the different items, which means when we evaluate this, SUBSTITUTE will spit out lots of answers. Now, comma, old text, I'm still looking for kg in double quotes, comma, I'm still substituting nothing, close parentheses, plus Z.

If I hit Enter, it'll show me the first one but not all of them, because in an array formula like this that's delivering many items, you can't display them all at once in one cell. But check this out: I can highlight this and, in edit mode, hit the F9 key to prove to myself that it actually successfully converted all of those to proper numbers. Control Z.

Now my goal is to add them. If I put them inside the SUM function, because that's an array operation, I'd have to do the special keystroke, Control Shift Enter, and I don't want to do that. I know that there's another function besides SUM, which is SUMPRODUCT. Normally SUMPRODUCT takes multiple arrays, multiplies all the arrays, and then adds. But guess what: that array argument can totally understand that array operation, so by only using one array, it will only do the sum. So anytime you have an array operation and your goal is to add, you can simply put it in SUMPRODUCT and hit Enter, and just like that, we get our correct answer.

Now, you don't want all these, so you highlight the top and hit the Delete key, and instantly we've gone from a column of numbers and text to adding just the numbers. Over here, we went ahead and added all of these with a custom number format, but we didn't have to do a special formula, because the SUM function is looking under the number formatting at the actual numbers. All right, we'll see you next trick.