How to Sum by Formula if Cell Contains Both A and B in Excel

In previous articles, we have introduced the method of sum numbers for cells which are equal to a certain value or are not equal to a certain value (you can refer to ‘How to Sum if Cell Contains Text in Another Column’, ‘How to Sum Numbers by Formula if Cells Are Not Equal to Certain Value’ on our website). In this article, we will show you the how to ‘sum if cell contains both A and B’ by formula with the help of Excel SUMIFS function.

Through demonstrate a simple instance, we will introduce you the syntax, arguments of SUMIFS function, and let you know how this formula works to reach your goal. After reading the article, you may have a simple understanding of SUMIFS function.

EXAMPLE

How to Sum by Formula if Cell Contains Both A and B in Excel 1

Refer to above table, we can see that some kinds of fruit are listed in “Fruit” column. At least two fruits are displayed in one cell. Total amount for fruit combination is listed in “Amount” column accordingly.

In this instance, we want to calculate total amount for fruit combination that contains “Apple” and “Orange”. So, no matter “Apple, Orange” or “Apple, Orange, Grape”, they are both seem to match the criterion. We need to find out all proper fruit combination, and then sum up values in “Amount” column. To fix this issue by formula, we can apply SUMIFS function here.

FORMULA – SUMIF FUNCTION

Step 1: Select A2:A9, then in Name Box define a new name for this range, for example ‘Fruit’.

How to Sum by Formula if Cell Contains Both A and B in Excel 2

Step 2: Select B2:B9, in Name Box define a new name for this range, for example ‘Amount’.

How to Sum by Formula if Cell Contains Both A and B in Excel 3

Step 3: In E2, enter the formula =SUMIFS(Amount,Fruit,”*Apple*”,Fruit,”*Orange*”).

How to Sum by Formula if Cell Contains Both A and B in Excel 4

Step 4: Press Enter after typing the formula.

How to Sum by Formula if Cell Contains Both A and B in Excel 5

We can see in column A, cell A2, A4 and A8 contains both “Apple” and “Orange”, the corresponding amounts are 100 (in cell B2), 200 (in cell B4), and 150 (in cell B8), so the total is 100+200+150=450. The formula works correctly.

SUMIFS FUNCTION INTRODUCTION

SUMIFS function can be seen as SUM+IFS, it can handle multiple ‘criteria range’ and ‘criteria’ combinations.

For SUMIFS function, the syntax is:

SUMIFS(sum_range, criteria_range1, criteria1, [criteria_range2, criteria2], …). Contents in [] are optional.

SUMIFS function supports wildcards like asterisk ‘*’ and question mark ‘?’, it also supports logical operators within its arguments. If wildcards or logical operators are required, they should be enclosed into double quotes (““) with text.

The usage of wildcards:

  1. An asterisk (*) means one or more characters.
  2. A question mark (?) means one character.
  3. The position of asterisk or question mark means the character(s) position relative to the entered part.

The usage of logical operators:

  1. “>” – greater than
  2. “<” – less than
  3. “<>” – not equal to

ALL ARGUMENTS

SUMIFS – SUM RANGE

In our instance, B2:B9 is the ‘sum range’ obviously. Amount values are listed in this field. We define this range with name ‘Amount’ in above step#2.

In the formula bar, select ‘Amount’, press F9, values in this range are listed in an array.

How to Sum by Formula if Cell Contains Both A and B in Excel 6

SUMIFS – CRITERIA RANGE 1

A2:A9 is the criteria range. In this instance we have only one criteria range. This range contains several kinds of fruit.

In the formula bar, these fruits are displayed in proper order in an array:

How to Sum by Formula if Cell Contains Both A and B in Excel 7

SUMIFS – CRITERIA 1

We want to calculate total amount for combinations contain “Apple” and “Orange”, so we will supply two criteria in this case, the first one is “Apple”. As there might be some characters listing before or after “Apple”, so we need to add “*” before and after “Apple”. As SUMIFS function supports texts and wildcards, but they should be enclosed into double quotes “ “ when applying them, so for criteria 1, we enter “*Apple*” in this position.

How to Sum by Formula if Cell Contains Both A and B in Excel 8

SUMIFS – CRITERIA RANGE 2

We have only one criteria range. So in criteria range2 position, we still enter “Amount”.

How to Sum by Formula if Cell Contains Both A and B in Excel 9

SUMIFS – CRITERIA 2

In criteria 1 we enter “*Amount*”, so in criteria 2 we enter “*Orange*”. Then the two criteria are both included in this formula.

How to Sum by Formula if Cell Contains Both A and B in Excel 10

HOW FORMULA WORKS

After explaining each argument in the formula, now we will show you how the formula works with these arguments.

Refer to above mentioned arguments, we can get three arrays and two texts within double quotes, the formula is converted into below format in the formula bar.

=SUMIFS({100;120;200;150;130;230;150;250},{“Apple, Orange, Grape”;”Banana, Orange”;”Orange, Apple”;”Apple, Banana”;”Banana, Orange, Grape”;”Orange, Banana”;”Apple, Grape, Orange”;”Banana, Grape”},”*Apple*”,{“Apple, Orange, Grape”;”Banana, Orange”;”Orange, Apple”;”Apple, Banana”;”Banana, Orange, Grape”;”Orange, Banana”;”Apple, Grape, Orange”;”Banana, Grape”},”*Orange*”)

See screenshot below:

How to Sum by Formula if Cell Contains Both A and B in Excel 11

There are two pairs of criteria range and criteria:

Criteria Range 1: {“Apple, Orange, Grape”;”Banana, Orange”;”Orange, Apple”;”Apple, Banana”;”Banana, Orange, Grape”;”Orange, Banana”;”Apple, Grape, Orange”;”Banana, Grape”}

Criteria 1: “*Apple*”

Criteria Range 2: {“Apple, Orange, Grape”;”Banana, Orange”;”Orange, Apple”;”Apple, Banana”;”Banana, Orange, Grape”;”Orange, Banana”;”Apple, Grape, Orange”;”Banana, Grape”}

Criteria 2: “*Orange*”

In criteria range 1, compare each fruit combination in the array with criterion “Apple”; if “Apple” exists in fruit combination, mark them in bold:

{“Apple, Orange, Grape“;”Banana, Orange”;”Orange, Apple“;”Apple, Banana“;”Banana, Orange, Grape”;”Orange, Banana”;”Apple, Grape, Orange“;”Banana, Grape”}

So, for the combination which meets the criteria, record a ‘True’ in the array; otherwise, record a ‘False’. Then we can get a new array:

Array 1: {True;False;True;True;False;False;True;False}

In criteria range 2, compare each fruit combination in the array with criterion “Orange”; if “Orange” exists in fruit combination, mark them in bold:

{“Apple, Orange, Grape“;”Banana, Orange“;”Orange, Apple“;”Apple, Banana”;”Banana, Orange, Grape“;”Orange, Banana“;”Apple, Grape, Orange“;”Banana, Grape”}

Then we get another array:

Array 2: {True;True;True;False;True;True;True;False}

As the two criteria “Apple” and “Orange” both should be included in the fruit combination, so we need to look up the intersection of array1 and array2, only True is marked in the same position in two arrays, True will keep in array. So, after comparing the two arrays, we get a new array3:

Array 3: {True;False;True;False;False;False;True;False}

Convert ‘True’ to ‘1’ and ‘False’ to ‘0’:

Array 4: {1;0;1;0;0;0;1;0}

Now, we have below two arrays:

Sum range: {100;120;200;150;130;230;150;250}

Array 4: {1;0;1;0;0;0;1;0} – from above steps we know that if cell contains both “Apple” and “Orange”, 1 is displayed.

We list the two arrays in two rows, and multiply the two numbers in the same column, and save their product in sum total.

Above all, numbers which are not zero in sum total row are the amounts of cell contains both ‘Apple’ and ‘Orange’. Now, sum up all numbers in sum total.

How to Sum by Formula if Cell Contains Both A and B in Excel 12

100+200+150=450

COMMENTS

In this case, if we forgot to add “*” in criteria, let’s see what do we get.

How to Sum by Formula if Cell Contains Both A and B in Excel 13

In fact, an asterisk (*) means one or more characters, if * is omit in criteria, that means cell which is exactly equal to the criteria can satisfy the condition. As we all know, a cell cannot either be ‘Apple’ or ‘Orange’. So, if we enter =SUMIFS(Amount,Fruit,”Apple”,Fruit,”Orange”) to sum numbers, it doesn’t work.

SUMMARY

1. SUMIFS function can handle multiple groups of criteria ranges and criteria. Sum range is the first argument among all arguments.

2. It supports user defined range name.

3. It supports wildcard.

4. It supports logical operators.

Related Functions


  • Excel SUMIF Function
    The Excel SUMIF function sum the numbers in the range of cells that meet a single criteria that you specify. The syntax of the SUMIF function is as below:=SUMIF (range, criteria, [sum_range])…
  • Excel SUMIFS Function
    The Excel SUMIFS function sum the numbers in the range of cells that meet a single or multiple criteria that you specify. The syntax of the SUMIFS function is as below:=SUMIFS (sum_range, criteria_range1, criteria1, [criteria_range2, criteria2], …)…

 

 

Related Posts
How to Sum if Equal to Many Items or A Range in Excel
How to Sum if Equal to Many Items 12

If we want to sum numbers from a range with criteria, we often select SUMIF of SUMIFS functions as the first choice to create a formula. The criteria can be a number or an array or a collection of some ...

How to Sum if Equal to X or Y in Excel
How to Sum if Equal to X or Y 19

In daily work, if we want to sum numbers from a range, and only sum the numbers which being equal to X or Y in the range, we can create a formula with Excel build-in functions to get the result. ...

How to Sum if Contains an Asterisk
How to Sum in Last N Days 14

In our daily life, we may want to sum amounts or sales for a specific period, for example in last N days. Sum numbers in Excel is easy to run, we can apply SUM function. But if we want to ...

How to Sum if Contains an Asterisk
How to Sum if Contains an Asterisk 7

To add numbers together we need to apply SUM function. And if we want to add numbers based on some conditions, we can add criteria with the help of SUMIFS function, SUMIFS can filter data with multiple criteria effectively. If ...

How to Sum in Vertical Range
How to Sum in Vertical Range 8

If we want to add numbers based on some conditions in Excel worksheet, we can add criteria with the help of SUMIFS function, SUMIFS can filter data with multiple criteria effectively. In this article, we will introduce you the method ...

How to Sum in Horizontal Range
How to Sum in Horizontal Range9

To add numbers together we need to apply SUM function. But if we want to add numbers based on some conditions, we can add criteria with the help of SUMIFS function, SUMIFS can filter data with multiple criteria effectively. In ...

How to Sum with Criteria and Or Logic in Excel
How to Sum with Criteria and Or Logic in Excel 8

To add numbers together we need to apply SUM function. And if we want to add numbers based on some conditions, we can add criteria with the help of SUMIFS function, SUMIFS can filter data with multiple criteria effectively. If ...

How to Subtotal Values for Groups and Only Keep One Subtotal for A Group in Column
How to Subtotal Values for Groups and Only Keep One Subtotal for A Group in Column14

Sometimes we may meet the case that subtotal values for different groups and only record a subtotal value for one group in a column, for other cells in this group, keep them blank. Today we will introduce you how can ...

How to Sum by Formula If Cells Are Not Blank in Criteria Range
How to Sum by Formula If Cells Are Not Blank in Criteria13

Sometimes we may meet the cases that some blank cells exist in criteria range or sum range. In most situations we will ignore them, so we need to filter data by ‘not blank/not empty’. Today we will introduce you how ...

How to Sum by Formula if Cell Ends with in Excel
How to Sum by Formula if Cell Ends with in Excel14

We have introduced the method of sum numbers based on criteria “sum if cell begins with” (you can refer to ‘How to Sum Data if Begins with in Excel’ on our website). In this article, we will show you the ...

Sidebar