The most versatile solution I know is using the SUM function and a dynamic range which in our case is $B$2:B2. However, the most important fact is that we have to lock (F4 key) the first part of this range (Fig. 1).
=SUM($B$2:B2)
Fig. 1 SUM function
When we copy our formula down, we can see that the first part of the range will always refer to cell B2, but the second one will go down as we drag the formula. This way, we have a range that expands as we go down. It’s the most versatile solution (Fig. 2).
=SUM($B$2:B4)
Fig. 2 An expanding range
This solution has problems with Excel tables. When we sum from F2 to F2 cells and press F4 key to lock it, everything looks fine. However, when we add new data to our table, the last cell will expand because it refers to the whole column July -> August (Fig. 3).
Fig. 3 Adding a new value
In this case, we have to modify our range. Instead of cell F2 we can use the table nomenclature. When we click cell F2, Excel will refer to this table row, where @ means this table row, and Income means the column we are referring to (Fig. 4).
=SUM($F$2:[@Income])
Fig. 4 Range modification
We have to overwrite all cells (Fig. 5).
Fig. 5 Overwriting cells
This way we got proper results. We can check if it works properly by adding some new rows. After we added two rows, the results are still correct (Fig. 6).
Fig. 6 Correct formula operation
The only drawback of this formula is that we won’t see the whole range because Excel won’t select the whole range. Theoretically, the last cell should include the whole column but Excel selects only the first and the last row (Fig. 7).
=SUM($F$2:[@Income])
Fig. 7 The formula in the last cell
We won’t see it, but Excel will. We have to remember that Excel will create a proper reference and we can create a proper running total, even in Excel tables.
If you want to calculate a median value with a condition, you cannot use a simple MEDIAN function because it needs only numbers. We have to use another function that will reduce the number of wages we look at.
Median with condition
Typically, when we want to add a condition to another function we can use the IF function and make a logical test. In our case, the logical test will be simple. We just have to check each cell in the Department column whether it’s equal to the name of the Department we look at now. Since we are comparing a single cell to a whole range, Excel will check every single cell in a given range (Fig. 1).
Fig. 1 Comparing a single cell to a range of cells
If a cell in the range is equal to the target cell, we want the function to return proper wages, i.e. the wages from the same row. If the logical test result is FALSE, we want to have something that the MEDIAN function will ignore which, in most cases, is an empty text string. However, I want to show you that the IF function really returns something so let’s write “no” (Fig. 2).
=IF($B$2:$B$13=E2,$C$2:$C$13,“no”)
Fig. 2 IF function
We can see that Dynamic Array Excel spilled the results and we have wages only for the departments we chose. For other departments we have ‘no’. Now, we can put this function into the MEDIAN function (Fig. 3).
=MEDIAN(IF($B$2:$B$13=E2,$C$2:$C$13,“no”))
Fig. 3 MEDIAN function with IF function
Now, we’ve calculated the median value with a condition for each department. We can see that for HR it’s $4,000, for IT it’s $8,100 and for Marketing it’s $5,900. Those are the middle values for each department (Fig. 4).
Why do politicians lie to us when talking about average wages? Let’s ask Excel.
Median vs Average wages
Let’s calculate some average. The AVERAGE function is a simple function that sums up values from all cells in a range and divides it by the number of cells with values (Fig. 1).
=AVERAGE(D2:D12)
Fig. 1 AVERAGE function
In our example, the average is $2,900. It means that 8 people earn less, which gives us around 80% of all people. It’s important that one person at the bottom earns much more than the rest and affects our average more than other people (Fig. 2).
Fig. 2 Average wages
If we want to talk more precisely about the distribution of those wages, we can use the MEDIAN function which returns the value in the middle (Fig. 3).
=MEDIAN (D2:D12)
Fig. 3 MEDIAN function
Since our values are sorted, we can clearly see the middle value, which is $2,000. We can see that 50% of people earn this value or less and 50% of people earn this value or more (Fig. 4).
Fig. 4 Median value
If we have an even number of values and there are two middle values, the MEDIAN function calculates the average of those two values near the middle line (Fig. 5).
=MEDIAN(G2:G11)
Fig. 5 MEDIAN function with even number of cells
We can see that it calculated the middle value (Fig. 6).
Fig. 6 Median value
If we add one zero to cell D12 which belongs only to one person, we can see that the average hit $11,900. It means that only one person earns more that the average. On the other hand, the median value stayed the same. It’s very important to know this difference (Fig. 7).
Fig. 7 Higher average
I also want to add one column with the RAND function (Fig. 8).
=RAND()
Fig. 8 RAND function
I want to show you that we don’t have to have our data sorted to use the MEDIAN function (Fig. 9).
We can count the number of spaces between words. Let’s have a look at our first example. In cell A2, we have spaces between words and at the end of each line, so the case is simple. We can use the LEN function here that counts the length of text (Fig. 1).
=LEN(A2)
Fig. 1 LEN function
Now, that we have the number of signs, we can remove spaces. We can use the SUBSTITUTE function here. This function will change the old text, which in our case is a space into new text. And since we want to remove spaces, our new text will be an empty text string written in double quotes (Fig. 2).
=SUBSTITUTE(A2,” “,“”)
Fig. 2 SUBSTITUTE function
We can see that the function removed all spaces from our text (Fig. 3).
Fig. 3 No spaces
Now, we can count the length of the text without spaces using the LEN function (Fig.4).
=LEN(SUBSTITUTE(A2,” “,“”)
Fig. 4 Counting the text length
Now we know the length (Fig. 5).
Fig. 5 Text length
Since we know how many spaces there are, we can use the formula to count the words. We just have to subtract this formula from the previous one (Fig. 6).
=LEN(A2)-LEN(SUBSTITUTE(A2,” “,“”))
Fig. 6 Subtracting formulas
And we have the number of spaces (Fig. 7).
Fig. 7 Number of spaces
We still have to remember that to count the number of words, we have to add 1 because there is only one space between two words (Fig. 8).
=LEN(A2)-LEN(SUBSTITUTE(A2,” “,“”))+1
Fig. 8 One space between two words
Now we know the number of words in each cell.
Fig. 9 Number of words
If we want to count words from all cells, we can use almost the same formula. The only thing we have to change is the range which now will be from A2 to A4 (Fig. 10).
=LEN(A2:A4)-LEN(SUBSTITUTE(A2:A4,” “,“”))+1
Fig. 10 Adding a new range
In Dynamic Array Excel, the results will be spilled, however we can sum the up (Fig. 11).
Fig. 11 Split results
In the previous version of Excel we can use the the SUMPRODUCT function (Fig. 12).
Count cells with text, COUNTIF function and wildcards
For me, the simplest solution is using the COUNTIF function and wildcards. We have an asterisk that represents any text string, even an empty one. We also have a question mark that represents just one single sign (Fig. 1).
Fig. 1 Wildcards
Let’s start with counting all cells with text. First, we have to select a proper range and press the F4 key to lock it. Then, we have to write an asterisk in double quotes so that it counts any text string, even an empty one (Fig. 2).
=COUNTIF($B$2:$B$11,”*”)
Fig. 2 Proper range and an asterisk
And just like that we have 4 cells with text. In cell B6 I have pasted an empty text string and even if we see nothing, Excel counts it as a cell with an empty text string. But, what can we do if we don’t want to count empty text strings? We can copy our formula and add a question mark next to the asterisk. It doesn’t matter if it’s before or after the sign (Fig. 4).
=COUNTIF($B$2:$B$11,”?*”)
Fig. 4 Formula modification
This way we counted all cells that have at least one sign (Fig. 5).
Fig. 5 Results
If we want to count all not empty cells, we can change our criteria to the <> signs (Fig. 6).
=COUNTIF($B$2:$B$11,”<>”)
Fig. 6 Adding <> signs
However, the simplest solution in most cases would be using the COUNTA function, where we don’t have to worry about any criteria (Fig. 7).