The spreadsheet below shows a list of names with the answer in cell D2. *Ross* is the name that occurs the least in that list.

This formula returns the least frequent value from the list in A2:A16. The formula is explained below so keep reading.

{=INDEX(A2:A16,MATCH(MIN(COUNTIF(A2:A16,A2:A16)),COUNTIF(A2:A16,A2:A16),0))}

This formula is an array formula so you need to press **Ctrl + Shift + Enter**, and not **Enter**. This will put the curly braces around the formula. You do not type these.

Within this formula the COUNTIF functions are used to return how many times each name occurs in the list. The COUNTIF functions return the result below;

{2;5;5;4;4;5;4;4;5;2;4;5;4;4;4}

This means that the name in the first cell of that range (A2) occurs twice, 2nd cell (A3) occurs five times, 3rd cell (A4) occurs five times and so on.

The MIN function returns the smallest number from that array, which is 2 in this example.

The MATCH function is then used to search for the position of the first instance of 2 (the least mentioned names position). The result of this is 1, because the first instance of 2 is in the first cell of range A2:A16.

The INDEX function then returns the value which is in that cell (A2). Which in this example is *Ross*. Watch the video below for a visual explanation of this formula.

The INDEX and MATCH functions are awesome when used together for a flexible lookup formula. Find out more at this INDEX and MATCH tutorial.

Seriously improve your Excel Formula skills with our online course. Over 100 formulas covered. Sign Up Now.

]]>We can find out how many cells by filtering the list by colour, and maybe use the AGGREGATE function to return the count. This is an option. But having to filter the list each time may not be good enough.

Because functions such as COUNTIF cannot count by cell colour, we will need to create our own custom function (also known as User Defined Functions or UDF’s) to get the job done.

- Open the Visual Basic Editor by pressing
**Alt +F11**or by clicking the**Visual Basic**button on the**Developer**tab. - Insert a new module if necessary by clicking the
**Insert**menu and then**Module**. - Copy and paste the code below into the code window (you can put this into the current workbook or the Personal Macro Workbook for global use).

Function COUNTIFCOLOUR(Colour As Range, rng As Range) As Long Dim NoCells As Long Dim CellColour As Long Dim rngCell As Range CellColour = Colour.Interior.Color For Each rngCell In rng If rngCell.Interior.Color = CellColour Then NoCells = NoCells + 1 End If Next COUNTIFCOLOUR = NoCells End Function

You can then use this function like any other function in Excel. The arguments do not appear like normal functions, but everything else is the same.

The user will select the range of cells to use and this is assigned to *rng*.

A For Next loop is then used to loop through each cell of that range checking if it is the colour we are looking for. If so 1 is added to the *NoCells* variable. This is assigned to the function for returning when the loop finishes.

Want to learn more Excel VBA? Check out the complete Excel VBA online course. You will see more examples of User Defined Functions and a whole lot more.

This is great when looking for a unique value. But what about when the value you are looking for appears multiple times in the list, and you want to return the last match.

Sure we could sort the list so that the last match would become the first, but this is not always an option.

This blog post looks at using the VLOOKUP function to return the last match in a list. The technique and formulas used can be adapted to find the 2^{nd} or 3^{rd} match if required.

In order to create a VLOOKUP to return the last match in the list, we will need to know how many matches there are in total.

The following formula has been entered in cell J1 to return how many times the customer ID occurs in the list.

=COUNTIF(C:C,$I$4)

Next we need to create a column of unique values which can then be used by the VLOOKUP function.

The values in this column are made by joining the customer ID and that instance of the ID. The image below shows column D as the helper column. You can see the two instances of customer *Hanari* *Carnes* which has the ID *1094*. A number 1 and 2 has been attached to the end of the ID to make it unique.

The following formula has been entered into column D. The position of the dollar signs is important for this formula to work.

=C2&COUNTIF($C$2:$C2,C2)

The COUNTIF function is an extremely powerful and versatile function to have in your Excel arsenal. Check out these 5 alternative examples of the COUNTIF function.

This column can be hidden once the VLOOKUP is written. It is an important column, but it does not need to be visible on screen.

With the helper column now in place, we can write a VLOOKUP to look to return the last match in a list.

The VLOOKUP function below has concatenated the contents of cell I4 (the customer ID) and J1 (the number of occurrences in the list of that customer ID) together to form the lookup value.

The table array is columns D:F to ensure that the leftmost column of the array is the helper column that we created.

=VLOOKUP(I4&J1,D:F,3,FALSE)

Want more information on VLOOKUP? Check out the ultimate guide to VLOOKUP.

]]>In this blog post will look into 5 more unorthodox but useful scenarios for the COUNTIF function to be used. The 5 COUNTIF examples we look at are;

- Prevent duplicates in a range.
- Uniquely rank items in a list.
- Count the unique entries in a list.
- Compare two lists.
- Identify names that occur 3 times or more.

Duplicate entries in a list is a common problem to encounter in spreadsheets. By using the COUNTIF function with the Data Validation tool in Excel though we can create a rule to prevent the entry of duplicate values.

In this example we will look at preventing duplicates in range A2:A10.

- Highlight the range of cells that you want to apply the validation rule to, for example A1:A10.
- Click the
**Data**tab and then**Data Validation**. - Click the
**Allow**list and select**Custom**. - Enter the formula below into the
**Formula**box provided.

=COUNTIF($A$2:$A:2,$A2)=1

This formula will check if the value being entered is already in the list (equal to 1), and if it is, the Data Validation tool will prevent it from being entered.

Notice the use of the dollar signs to fix the first part of the reference, whilst the row number of the second part of the range is left relative to check all entries in the list.

You can rank items in a list in Excel by using the RANK function. For example, you may want to create a league table for your sales team and rank their performance for the month.

However, if two salespeople sell the same amount they will have an equal rank. In the image below two salespeople are ranked in 5^{th} position and ranking 6 is skipped.

This probably makes sense and is a good thing because they did achieve the same. However if you are planning to create a league table from this data using VLOOKUP, it will not work without a unique ranking for each salesperson.

The formula below creates a unique ranking for each salesperson. It adds 1 onto the ranking if it already exists so that it is not duplicated.

=RANK($C2,$C$2:$C$9,1)+COUNTIF($C$2:$C2,$C2)-1

To learn more about creating league tables in Excel, check out our online course for creating sports league tables and competitions.

A common requirement in Excel is to compare two lists. There are many different techniques for this, but this article is about COUNTIF.

In the example below, we want to know what names in the second list ** do not** appear in the first list.

To do this the formula below was entered into cell D2 and then copied to the other rows. It counts how many times the name in the second list appears in the first.

=COUNTIF($A$2:$A$7,C2)

If it returns 0 then the name is missing from the first list. The list can be filtered, used in a PivotTable or have Conditional Formatting applied to work with the results better.

The COUNTIF function can also be used to create a distinct count (count of the unique entries).

The list below shows the number of visitors to a site. The goal is to find out how many unique visitors there were.

There is no function in Excel for counting unique, or distinct entries. However by combining the COUNTIF function with the brilliant SUMPRODUCT we can get what we want.

The formula below counts the number of unique visitors in the list.

=SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9))

In this formula, the COUNTIF function produces the result below;

{1;1;2;1;1;2;2;2}

This is because Justin Timberlake appears once, Mariah Carey once, Celine Dion twice and so on. So it is counting how many times each delegate attended.

These values are then divided by 1 so that when summed together we are adding 1 for each delegate. The array below is summed.

{1;1;0.5;1;1;0.5;0.5;0.5}

Excel provides a few built-in features for handling duplicates in a list including a Conditional Formatting rule (introduced in 2007). So you will not need the COUNTIF function for this.

However, you may only want to identify the records if they appear 3 times or more in the list, like in the image below.

You can write your own rules using formulas in Conditional Formatting and in this example the COUNTIF function is required.

- Select the list of names.
- Click the
**Home**tab,**Conditional Formatting**and then**New Rule**. - Select
**Use a formula to determine which cells to format**. - Enter the formula below into the box provided.

=COUNTIF($A$2:$A$13,A2)>=3

]]>This post looks at using a formula to calculate this distinct count.

Consider the list below of a list of delegates attending our courses. A normal count on this range will tell us how many attendances there were. That’s good, but we want to know how many unique attendees there were.

If you have not been introduced to the amazing world of the SUMPRODUCT function before then you are in for a treat. This function comes to our rescue on so many occasions.

The image below shows the formula to count the number of different delegates that attended.

The answer appears as 4. There are 4 different delegates (Mickey Mouse, Bill Ding, Belle Jinwaffles and Minnie Mouse).

**Explanation**

Ok, let’s try and explain what is going on here.

The COUNTIF function produces the result below;

{3;2;1;3;1;3;2}

This is because Bill Ding appears 3 times, Belle Jinwaffles twice, then Mickey Mouse once etc. So it is counting how many times each different delegate attended.

These figures are then divided by 1 so that when summed together we are adding 1 for each delegate. The array below is summed.

{0.33;0.5;1;0.33;1;0.33;0.5}

Not the easiest formula to get your head around. Most importantly it works. An understanding can come with time if it seems tricky right now.

Spaces in the range will cause the *#DIV/0!* error to appear.

The formula can be adapted to ignore any spaces in the range.

]]>