Summing Based on Part of a Control Cell

Written by Allen Wyatt (last updated January 29, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Eszter has a long list of cells in column A that contain a series of mutation codes, such as "AKT 142" or "BRAF 1975." In column B are values associated with these mutation codes. She needs a formula that will sum the values in column B for which the corresponding mutation code in column A begins with the same sequence, as in all those beginning with AKT or BRAF. Eszter suspects this can be done with the SUMIF function, but she doesn't know how to make it pay attention to only the first part of the mutation code.

There are many ways you could approach this problem, but in this tip, I focus on only three potential solutions.

Using a Helper Column

If your worksheet layout allows, you could add a helper column that contains only the first portion of the mutation codes. Since your mutation codes are in column A, you could insert the following formula in the first cell of your helper column:

=LEFT(A1, SEARCH(" ",A1,1)-1)

Copy it down for as many cells as necessary, and you end up with the helper column containing everything in the mutation codes before the space. You could then use your desired SUMIF formula to sum based on the contents of the helper column.

Using SUMPRODUCT

A rather unique approach to solving the problem is to use the SUMPRODUCT function. Let's say that you put, in cell E1, the preface code you are interested in. (So, for instance, you might put "AKT" into cell E1.) You could then calculate the desired sum by using the following formula:

=SUMPRODUCT(--(LEFT(A:A,LEN($E$1))=$E$1) * B:B)

This works because SUMPRODUCT examines whether the leftmost portion of a cell in column A matches whatever you put in cell E1. If it does, then the comparison returns 1; if it doesn't, it returns 0. This is then multiplied by the corresponding cell in column B and summed.

Using SUMIF Directly

Perhaps the cleanest approach is to simply use SUMIF directly. You know, from using the helper-column approach, that you can use SUMIF to look at the contents of a cell and then selectively sum another column. You do it in this general manner:

=SUMIF(Check_Range, Criterion, Sum_Range)

Thus, if you wanted to sum values in column B based on what is in column A, you could do the following:

=SUMIF(A:A, "AKT", B:B)

This would, of course, only match those cells in column A that contain just AKT. This is not Eszter's situation, though—the mutation codes in column A contain more than just AKT. This is where the use of wildcards in the criterion specification comes into play. All Eszter needs to do is to add an asterisk, in this manner:

=SUMIF(A:A, "AKT*", B:B)

Now SUMIF returns the proper sum based only on those cells in column A that begin with the letters AKT. It doesn't matter what follows the AKT characters in each cell because the asterisk says to Excel that it should "accept anything that follows those three characters."

You could even make this approach more general in nature. Let's assume that you put the desired preface code (the one on which you want to sum) into cell E1. You could then put the following into cell E2:

=SUMIF(A:A, E1 & "*", B:B)

Now, if E1 contains "AKT" you end up with a sum of values for that preface code. If you change E1 to "BRAF" then you get a sum for that preface code, without a need to change the formula in E2.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13614) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Refreshing PivotTable Data

If you modify the data on which a PivotTable is based, you'll need to refresh the table so it reflects the modified data. ...

Discover More

Filtering to a Date Range in the Past

If you have a large number of data records, each with an associated date, you might want to filter that data so you see ...

Discover More

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Generating Numeric Testing Data

Excel can be used to generate random testing data that you can use in any way you deem necessary. This tip provides ...

Discover More

Combinations for Members in Meetings

Got a large group of people listed in a worksheet and you want to make sure that each person has met with every other ...

Discover More

Extracting a Pattern from within Text

If you have a large amount of data in a worksheet and you want to extract information from the text that meets certain ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.