## Count identical values if they are on the same row

This article describes a formula that counts values in two columns if they are duplicates on the same row.

**What's on this page**

## 1. Count identical values if they are on the same row (Array formula)

Hi Oscar,Need a formula to count identical numbers in two columns but items must be in same row (position).

12 15

8 8 good count 1

22 19

7 22 for 22 not count cause is not in same row

14 14 good count 2

Array formula in cell E2:

### 1.1 How to enter an array formula

- Select cell E2
- Paste the formula in formula bar
- Press and hold CTRL + SHIFT simultaneously
- Press Enter
- Release all keys

Your formula now begins and ends with a curly bracket, if you did it right.

Like this {=SUM((A2:A6=B2:B6)*1)}

Don't enter the curly brackets yourself, they appear automatically.

### 1.2 Explaining formula

#### Step 1 - Compare values in column A with column B

The equal sign lets you compare value to value, the result is boolean value TRUE if they match (not case-sensitive) and FALSE if they don't.

A2:A6=B2:B6

becomes

{12; 8; 22; 7; 14}={15; 8; 19; 22; 14}

and returns

{FALSE; TRUE; FALSE; FALSE; TRUE}

#### Step 2 - Multiply boolean values with 1

To be able to sum the values in this array {FALSE; TRUE;FALSE;FALSE;TRUE} we need to convert the boolean values to their numerical equivalents, FALSE = 0 (zero) and TRUE = 1.

(A2:A6=B2:B6)*1

becomes

({FALSE;TRUE;FALSE;FALSE;TRUE})*1

and returns

{0; 1; 0; 0; 1}

#### Step 3 - Sum values in array

The SUM function adds all numbers in the array and returns a total.

SUM((A2:A6=B2:B6)*1)

becomes

SUM({0; 1; 0; 0; 1})

and returns 2.

### 1.3 Trim space characters

This formula also works with text values, to remove blanks before and after use TRIM function.

## 2. Count identical values if they are on the same row (Regular formula)

## 3. Count identical values on the same row comparing values in n columns

The formula in cell F3 counts the number of rows that contain the same value. In the example shown in the image above row 4, 7, and 9 contain the same value and the formula returns 3.

Note that this formula works with any cell range size, it does not need to be exactly three columns for this formula to work. However, you need to adjust the cell references accordingly in order to get a correct result.

### 3.1 Explaining formula

#### Step 1 - Compare values across columns

The equal sign lets you check if cell values match, the equal sign is a logical operator and returns a boolean (logical) value. TRUE if they match and FALSE if not.

B3:B10=B3:D10

becomes

{12; 8; 22; 7; 14; 5; 7; 11}={12,11,15; 8,8,8; 22,19,22; 7,8,22; 14,14,14; 5,2,6; 7,7,7; 11,18,12}

and returns

{TRUE, FALSE, FALSE;TRUE, TRUE, TRUE;TRUE, FALSE, TRUE;TRUE, FALSE, FALSE;TRUE, TRUE, TRUE;TRUE, FALSE, FALSE;TRUE, TRUE, TRUE;TRUE, FALSE, FALSE}

#### Step 2 - Convert boolean values

The MMULT function can't handle boolean values, we need to convert TRUE and FALSE to their numerical equivalents. TRUE - 1 and FALSE 0 (zero).

(B3:B10=B3:D10)*1

becomes

{TRUE, FALSE, FALSE;TRUE, TRUE, TRUE;TRUE, FALSE, TRUE;TRUE, FALSE, FALSE;TRUE, TRUE, TRUE;TRUE, FALSE, FALSE;TRUE, TRUE, TRUE;TRUE, FALSE, FALSE}*1

and returns

{1, 0, 0;1, 1, 1;1, 0, 1;1, 0, 0;1, 1, 1;1, 0, 0;1, 1, 1;1, 0, 0}

#### Step 3 - Calculate column numbers

The COLUMN function calculates the column numbers based on a cell reference.

COLUMN(B3:D10)

returns {2, 3, 4}.

Column B is 2, C is 3 and D is column number 4.

#### Step 4 - Convert all numbers to number 1

This step converts all column numbers to number 1. This is done by taking each number in the array raised to the 0 (zero) power.

COLUMN(B3:D10)^0

becomes

{2, 3, 4}^0

and returns {1, 1, 1}.

#### Step 5 - Convert a horizontal range to a vertical range

The TRANSPOSE function allows you to convert a vertical range to a horizontal range, or vice versa.

TRANSPOSE(COLUMN(B3:D10)^0)

becomes

TRANSPOSE({1, 1, 1})

and returns {1; 1; 1}.

The colon and semicolon tell you if an array is arranged vertically or horizontally. This is determined by your computer's regional settings.

#### Step 6 - Calculate the number of matches per row

The MMULT function calculates the matrix product of two arrays, an array as the same number of rows as array1 and columns as array2.

MMULT(*array1*, *array2*)

MMULT((B3:B10=B3:D10)*1,TRANSPOSE(COLUMN(B3:D10)^0))

becomes

MMULT({1, 0, 0;1, 1, 1;1, 0, 1;1, 0, 0;1, 1, 1;1, 0, 0;1, 1, 1;1, 0, 0}, {1; 1; 1})

and returns {1; 3; 2; 1; 3; 1; 3; 1}.

#### Step 7 - Check if the number of matches is equal to the number of columns

MMULT((B3:B10=B3:D10)*1,TRANSPOSE(COLUMN(B3:D10)^0))=COLUMNS(B3:D10)

becomes

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

and returns

{FALSE; TRUE; FALSE; FALSE; TRUE; FALSE; TRUE; FALSE}.

#### Step 8 - Convert boolean values

The SUMPRODUCT function can't work with boolean values, we need to convert them to their numerical equivalents. TRUE - 1 and FALSE - 0 (zero).

(MMULT((B3:B10=B3:D10)*1,TRANSPOSE(COLUMN(B3:D10)^0))=COLUMNS(B3:D10))*1

becomes

{FALSE; TRUE; FALSE; FALSE; TRUE; FALSE; TRUE; FALSE}*1

and returns {0; 1; 0; 0; 1; 0; 1; 0}.

#### Step 9 - Add numbers and return total

SUMPRODUCT((MMULT((B3:B10=B3:D10)*1,TRANSPOSE(COLUMN(B3:D10)^0))=COLUMNS(B3:D10))*1)

becomes

SUMPRODUCT({0; 1; 0; 0; 1; 0; 1; 0})

and returns 3.

Count unique distinct values that meet multiple criteria

This post demonstrates how to build an array formula that counts unique distinct values based on criteria. What's on this […]

Count Conditionally Formatted cells

This article explains how to count cells highlighted with Conditional Formatting (CF). The image above shows data in cell range […]

This article describes how to count unique distinct values. What are unique distinct values? They are all values but duplicates are […]

Count cells between specified values

This article demonstrates formulas that calculate the number of cells between two values, the first scenario involves two search values […]

Count unique distinct values in two columns

Formula in C12: =SUM(1/COUNTIF($B$3:$B$8, $B$3:$B$8))+SUM(IF(COUNTIF($B$3:$B$8, $D$3:$D$8)=0, 1/COUNTIF($D$3:$D$8, $D$3:$D$8), 0)) How to create an array formula Double press with left mouse […]

Count unique distinct values in a filtered Excel defined Table

This article demonstrates a formula that counts unique distinct values filtered from an Excel defined Table. Debra Dalgleish described in […]

How to count unique distinct values based on a date

The array formula in cell D3 calculates the number of unique distinct items based on the given date in column B. […]

Count unique distinct values within same week, month or year

The array formula in cell E3 counts unique distinct items for all dates within the same week. Example, week 2 […]

This article demonstrates formulas that count values in cell based on a delimiting character. The image above shows a formula […]

Count unique distinct values based on a condition

This article demonstrates how to construct a formula that counts unique distinct values based on a condition. The image above […]

### 3 Responses to “Count identical values if they are on the same row”

### Leave a Reply to Indigo

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

You can use non array formula, like this:

=SUMPRODUCT((A2:A6=B2:B6)*(A2:A6/B2:B6))

Try to find another non array formula.

There is a number of ways to make such a count as non array formula, e.g. =SUM(INDEX((A2:A6=B2:B6)*1,,)). But why not use array formula if it does the job?

[quote]Leonid says:

April 12, 2016 at 6:49 pm

There is a number of ways to make such a count as non array formula, e.g. =SUM(INDEX((A2:A6=B2:B6)*1,,)). But why not use array formula if it does the job?[/quote]

I suspect that Kidd has many more rows and columns in his file and CSE formulas are eating resources.