Welcome to Excel Magic Trick number 1,369. Hey, if you want to download this Excel file so you can follow along, click on the link below the video. Hey, we have an amazing video here, and I couldn't figure out which title to use either: COUNTIFS to count entered results for a particular shift in a date range, or COUNTIFS to count not-empty cells with four criteria or conditions. Now, this question comes from Rows at YouTube, and here's the question: here's the data set. Too formula, which will count nonblank cells in column C. That's this column right here. Nonblank means something has been entered, if column A equals a particular item and column B equals a particular item. More specifically, a formula which will count the number of results entered for a particular date range on a particular shift. I scroll down here is an example of the data set: date, shift, result. Because we want a particular date range, we're going to have to have a start date and an end date, and we're also going to have to have a shift number. Now, notice that's one, two, three conditions. Not only that, but the fourth condition will be: is there something entered in the result column? So this is going to be the COUNTIFS function. Now, what's great about COUNTIFS is it can count with one or more conditions. You just have to put the criteria range, then the criteria, next criteria range, next criteria.
Now, for criteria range one and two, we have to ask two different questions of the same date column. We need to ask of every date in this column: are you greater than or equal to the start date, and are you less than or equal to the end date? So for our criteria range I'm going to enter this whole column, but watch this. I'm actually going to click Escape, because I want to show you a great trick. Since I'm going to have so many ranges to enter, why don't we just give this column the name "date," this column the name "shift," and so on? We could manually do it. I could highlight this, click in the Name Box, and type "date." Then I would have a name that I could find in the Name Manager and use in any formula. But there's an awesome trick: if you have the names of your columns above the columns, you can simply highlight the names at the top of the columns, go up to Formulas, over to the Defined Names group, and click Create from Selection. I do this so often I know the keyboard shortcut, and you can see it there in the screen tip: Control+Shift+F3. So I'm going to press Control+Shift+F3. When you do that, you've got to be careful. It wants to be opened, and it thinks we want to name each row also, and I definitely don't want that. I just want the top row. Click OK. Now watch this: I can use the drop-down and select "date." Oh, that is so cool. I could actually use this as a Go To feature, but now we're just going to use those names in our formulas.
For our first range, I'm going to type "date," and look at that, there's even an icon for our defined name, a gold dog tag. So I'm going to down arrow and tab, so there we have our first range, comma. Now the criteria: we need to ask the question, are you greater than or equal to the start date? When you have a comparative operator and you're joining it to a formula input in a cell, you have to put the comparative operator in double quotes: greater than or equal to. Notice there's no single character for greater than or equal to like there is in math. We have to put two characters in: the greater-than sign, then the equal sign. The equal sign always comes after the greater-than or less-than sign, inside the double quotes. Now we need to join that, so we use the join symbol, the ampersand, which is Shift+7. Then I simply click on the start date. That's a relative cell reference, so as I copy the formula down, we'll always get greater than or equal to whatever is in this cell. Sometimes when you're learning comparative operators for the first time, this construction is hard, but notice the bigger side is pointing away from this cell. So it's actually pointing toward the whole date column, and it asks the question of every single date: are you greater than or equal to that particular date?
Now we enter a comma, criteria range two, "date," down arrow, tab, comma, and criteria two in double quotes: less than or equal to, then double quotes, and an ampersand to join it to our end date. Now we have equal signs on both our lower and upper limits. That's because the start date and the end date should both be included in this particular interval.
All right, comma. Criteria range three: I highlight the entire shift column. Notice I can enter the defined name either by typing, like we did over here, or simply by highlighting it, and it knows that range is now called "shift." Comma, relative cell reference, that's the shift number, comma. Criteria range four: I'm going to highlight the result column, and there's "result," then a comma.
Now we have two ways to do this. The first way is we assume we're always entering numbers zero or greater. So for our criteria in double quotes I'm going to enter greater than or equal to, and since we're assuming this condition will never change, we're just going to type it into the formula. Notice the difference here and over here: if the number will never change, you still have to put the comparative operators in double quotes, but also the number over here, because we definitely want the ability to change that. As a formula input, because it's coming from a cell, we have to use a cell reference, then we join it. That will work. Close parentheses, Control+Enter.
Now I can come over to the little green square in the lower right-hand corner of the cell. That's called the fill handle. When I move my selection cursor over it, it changes to a crosshair. I can double-click it, and it sends the formula down, copying it all the way down until it sees an empty cell. I click in the last cell and press F2 to put it in edit mode. I'm verifying that all the cell references are looking in the correct place, and they are. We could manually verify that each count is correct. I'm going to highlight the entire range for our first lower and upper dates for shift one. It looks like one, number two, number three, number four, number... how about this count of three for shift two? One, two, and there's an empty cell three. So it looks like it's working.
Now, what if I wanted to ask the question not as a number greater than or equal to zero, but as: is the cell really not empty? Maybe I entered the word "to" here. Well, I'm not going to retype the whole formula. Let's copy this. In edit mode, Control+C, Escape, F2 to put it in edit mode, Control+V. Everything's the same except criteria four. The criteria we enter is "say is the cell not empty": double quotes, then the comparative operator construction for "not equal," which is a less-than sign and then a greater-than sign, typed one after the other, then double quotes. Yes, that seems strange, but COUNTIFS always interprets that as not empty. Close parentheses, Control+Enter, double-click, and send it down. We get the same count here because they're all numbers. But if I type "two" here, that one is going to count, because we assumed the entries in that column would be greater than or equal to zero. This one works. Not only that, but if we had minus one, that should be counted if the numbers were supposed to be like that. I don't think they were. I think it was always going to be zero or greater, so of course that one's not going to work. This one will. All right, I'm going to press Control+Z. There you go, a little fun with counting when results have been entered, given a particular date range and shift number. All right, we'll see you in the next video.
