This blog post explores some examples of using **wildcard characters in formulas** to find, sum or count cells containing partial matches to what we are searching for.

If you prefer a video tutorial then check it out below, otherwise please continue for the written tutorial.

Before we look at some examples of wildcards in formulas, we should discuss the three types of wildcard characters you can use in Excel.

*** (asterisk)**– represents any number of characters. For example,*In**could mean India, Indonesia, Indianapolis, Innsbruck.**? (question mark)**– represents one single character. For example,*L?ndon*could mean London, or Landon.**~ (tilde)**– used to identify a wildcard character in the text. For example, if you wanted to find the exact phrase**London*in the text, you would enter*~*London*. Otherwise all entries ending with the word London would be returned because of the asterisk wildcard.

I assume we are all big fans of VLOOKUP. The most well known lookup function of Excel. But did you know that by using wildcard characters you can create a partial text match with VLOOKUP?

The following formula has been used in cell E3. The ampersand (&) has been used to concatenate the location in cell D3 with the asterisk wildcard. So this VLOOKUP will find the first value that begins with the text in D3.

Sometimes you do not know, or in this case do not need to know the full name of what you are looking for, and a partial match in itself is unique.

=VLOOKUP(D3&"*",$A$2:$B$10,2,FALSE)

Learn more in our Ultimate Guide to VLOOKUP.

The COUNTIF and SUMIF functions of Excel are also two of its most commonly used functions. It makes sense therefore that wildcards can be used with them.

In the example below we have a list of invoices and amounts. The first two characters of each invoice determine the sales rep involved.

We do not care about the exact invoice number right now. Just as long as the first two characters match the sales rep I am reporting on, then fantastic.

This COUNTIF function was used in cell E3 to calculate the number of orders by the sales rep SJ. In similar fashion to the preceding VLOOKUP example, cell D3 and the asterisk character are joined to create the criteria.

=COUNTIF(A2:A11,D3&"*")

This SUMIF function was then used in cell F3 to total the orders for SJ, or whatever sales rep is entered into D3.

=SUMIF(A2:A11,D3&"*",B2:B11)

In this example, we imagine a list of invoice numbers which should all be 6 characters in length. We want to test the range of invoice numbers to check if there are any inconsistencies.

The formula below uses a COUNTIF function and the question mark wildcard. By entering 6 question marks in a string we are counting the cells containing exactly 6 characters.

The answer in cell C2 is 7 indicating that there must be 3 that do not meet that criteria. Can you see them? Well in the next example we will get Conditional Formatting to highlight them.

=COUNTIF(A2:A11,"??????")

This formula can be added into a Conditional Formatting rule to identify the cells containing the incorrect text length.

The formula below has been used. It is slightly different to the COUNTIF in the previous example.

=COUNTIF(A2,"<>??????")

Because it is part of a Conditional Formatting rule, it only references the single cell (A2). This is the first cell of the range. The formatting is applied to the entire range of cells, but in the formula you reference just a single cell.

If you are not familiar with the “<>” symbol, that is the NOT operator. It has been included in the string with the six question marks. So this rule will format all the cells that do not have 6 characters.

Wildcard characters can be used in more functions that what I have demonstrated here. Using them with IF, FIND and SUBSTITUTE can be useful also.

If you can perform formulas on the full content of a cell that fantastic, but when you do not know the full content, wildcards do a great job of stepping up to the plate.

- Excel formula to display the sheet name in a cell
- Find the least frequent value in a list
- Prevent formulas from showing in the formula bar
- Pick a name at random from a list

]]>

In this tutorial we will use a list of 87 names that as we type into the drop down list, it searches the names, and the list shortens to show only those names containing that string of characters.

There are a few formulas to write to get this done. Everything is shown and provided in this tutorial. If you prefer a video. Check out the video tutorial below.

The drop down list used here is the combo box control. It is not a Data Validation list. So the first task is to insert a combo box control where you want the list to be.

- Click the
**Developer**tab on the Ribbon (don’t have a Developer tab? Right click the Ribbon, select Customise the Ribbon and check the Developer on the right). - Click the
**Insert**button in the Controls group and select the**Combo Box (ActiveX Control)**.

- Click and drag to draw this control onto the worksheet. You can always move and resize the control later, so don’t worry about 100% accuracy.
- Now we need to link the combo box value to a cell. After inserting the combo box, you should already be in design mode. Click the
**Properties**button on the Developer tab. If this is not active, click the**Design Mode**button and then**Properties**. - The Properties window appears. In the
**LinkedCell**property, type the cell address of the cell you want the combo box linked to.

- Let’s check out what we currently have. Click the
**Design Mode**button to exit design mode. Type into the combo box and you should see the value appearing in the cell you linked it to.

To be able to create this searchable list, the first formula will need to identify which names meet the criteria and should continue to appear in the list when the user is typing.

To identify the names that contain the characters being searched for, we will use an Excel function with a very appropriate name – the SEARCH function.

The SEARCH function below can be used.

=SEARCH($G$1,A2)

In this function, cell $G$1 is the cell that the combo box is linked to. So this contains the string of characters that we are looking for. Cell A2 is the first name in the list of names.

This function will return the *#VALUE!* error if a name does not contain the characters typed, and returns the first occurrence of those characters if it does.

In the image below you can see that the characters being searched occur in the fifth position of the names in A2 and A3, in position 1 of A6 and not at all in the other cells.

Now this is what we need. We now know which names should be appearing in the final drop down list while users are searching.

However, I don’t like the *#VALUE!* error and I’m not really interested in what position the characters are in. I just need to know that they are there.

The ISNUMBER function can then be added to the SEARCH function so that it returns TRUE or FALSE when a name is identified or not. The double negative is then added before the ISNUMBER function to convert the TRUE or FALSE to 1 or 0.

=--ISNUMBER(SEARCH($G$1,A2))

The next formula needs to count how many names in the list contain the characters being searched for.

This is important to create the dynamic aspect of the searchable drop down list. As characters are typed the list should shrink, and then expand again when the characters are deleted.

The COUNTIF function is used here to count the occurrences of names being returned. This function uses a reference to the ‘IF Found’ column in B. It has a fixed start point of $B$2, but a relative row number on the end point $B2. This ensures that the reference gets bigger as the formula is copied down.

It is counting how many times a 1 occurs in the range up to the point of the current row.

COUNTIF($B$2:$B2,1)

An IF function can then be added to it so that it only performs the COUNTIF, if the cell contains a 1, otherwise show blank in the cell.

=IF(B2=1,COUNTIF($B$2:$B2,1),"")

We now know which names should be appearing in the list, and we also know how many there are.

Next step is to create a range of cells containing these names only. A drop down list can then use this range of cells.

To lookup these names we can use the amazing INDEX and MATCH function combination.

=INDEX($A$2:$A$88,MATCH(ROWS($C$2:$C2),$C$2:$C$88,0))

The INDEX function is used here to return a name from the range A2:A88 from the row specified by the MATCH function.

The ROWS function is used to return the current row number. So for the first name in the list it is row 1 of that range. The second name is row 2 and so on. This works to provide MATCH with the current name we are looking for (1st, 2nd etc).

The MATCH function then looks for this number in range C2:C88. It reports back to INDEX with the names position, and INDEX extracts the name.

The IFERROR function can then be added to this so that the cell contains blank if there are no more names to look for.

=IFERROR(INDEX($A$2:$A$88,MATCH(ROWS($C$2:$C2),$C$2:$C$88,0)),"")

Now that we have the returned names, we need an easy way of referring to them. For this we will create a named range. This will need to be dynamic and detect how many names are returned. The combo box will then use this named range for its list.

Click the **Formulas** tab on the Ribbon and then **Define Name**. Enter a name for the range (I named it *employees*) and then enter the formula like below into the **Refers to** field.

=$D$2:INDEX($D$2:$D$88,COUNTIF($D$2:$D$88,"?*"))

To __create the dynamic named range__ we used the INDEX function with COUNTIF. The INDEX function is capable of returning a value or a reference and in this scenario we need it for the latter.

The dynamic range starts from $D$2 and then the COUNTIF function is used to locate the last name in the list for the INDEX function. The “?*” is used as the criteria to count the number of cells containing text.

Now that we have the searchable list of names in a column and the range has been named. The combo box needs to be told to use this named range as its source.

- Click the
**Developer**tab on the Ribbon and then the**Design Mode**button. - Now we are in design mode, click on the combo box control and then click the
**Properties**button. - Type the name that you gave your named range into the
**ListFillRange**field.

- Close the properties window.

The final step is to make sure the list drops down when somebody types into it. At the moment someone could click the list arrow to drop it down, but if they type, no list appears.

- Double click on the combo box (you need to still be in design mode for this).
- This will take you to a code window to enter a tiny piece of VBA code. Type the line below in between the
*Private Sub*and*End Sub*lines.

Private Sub ComboBox1_Change() ComboBox1.DropDown End Sub

Adjust the *Combobox1* part to the name of your combo box.

If you are interested in learning VBA then check out our Excel VBA online course. It is a fantastic skill to have if you are a heavy Excel user.

Exit design mode and test out your combo box. You should now have a searchable drop down list to use on your Excel reports, dashboards and forms. Everyone will be asking how you did it.

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.

]]>