# Summing When the First Character Matches a Value by Allen Wyatt
(last updated September 17, 2016)

George has a worksheet where the first row, in the range B1:AK1, contains part numbers. Some part numbers begin with X and others begin with Y. He wonders if there is a way to use SUMIF (or some other function) to sum the range B2:AK212 only for those columns in which the first cell in the column (B1:AK1) contains an "X" as the first character in the part number.

One way to accomplish this task is to use the SUMPRODUCT function along with the LEFT function to determine if the part number in the first row starts with an X or not:

```=SUMPRODUCT((LEFT(B\$1:AK\$1,1)="X")*B2:AK212)
```

The LEFT function returns the leftmost character of the part number and compares it to X. If it is equal, then the result is 1; if not equal then it is 0. This resulting value (1 or 0) is then multiplied by the individual cells in the data range. The result is your desired sum.

If you must use the SUMIF function for some reason, there are two ways you could approach the problem. First, you could add the following into cell AL2:

```=SUMIF(B\$1:AK\$1,"X*",B2:AK2)
```

This results in a sum of just the cells in row 2 that have a part number beginning with X. Copy the cell downward to cells AL3:AL212, and then sum the column.

The other approach is to add a totals row at the bottom of your data. Thus, you could use the following in cell B213:

```=SUM(B2:B212)
```

Copy this formula to the other cells on the row (C213 through AK213) and then you can use this formula to get your desired sum:

```=SUMIF(B1:AK1,"X*",B213:AK213)
```

In this case, SUMIF is checking the first row (where the part numbers are) and summing the appropriate cells from the totals you just added in row 213.

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

##### 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

Nudging an Equation

You can adjust where an equation is printed by moving it minute amounts in any direction.

Discover More

Adding a Drop Shadow to a Text Box

One way to make your text boxes "stand off" the page is to add a drop shadow to them. This tip shows just how easy it is ...

Discover More

Turning Off Insert Options

When you insert rows, columns, or cells in a worksheet, does the resulting Insert Options icon bother you? Here's how to ...

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)

Counting Cells with Specific Characters

Excel is used by many people to hold all sorts of data, not just numbers. If you have cells that include meaningful ...

Discover More

Counting Unique Values

Need to know a count of unique values in a range of cells? There are a couple of ways you can tackle the problem.

Discover More

Starting Out Formulas

When you enter a formula from the keyboard, Excel only knows it is a formula if you start it with an equal sign. You can ...

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}] 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 9 - 6?

2016-09-28 02:18:32

James Bruno

@ Lars Damgaard
Why do you use the double minus in your formula?
See Excel Gurus Gone Wild by Bill Jelen, Page 12 for a great example.

While IF and other Functions that expect logical tests can easily convert TRUE and FALSE values to 1's and 0's, the SUMPRODUCT cannot do this.
You need a way to convert the TRUE and FALSE values to 1/0 values. Use the Minus Minus to coerce excel to change an array of TRUE/False to 1s and 0s

2016-09-26 04:47:16

Lars Damgaard

Thanks, Peter,
Why do you use the double minus in your formula? For me, it works just as well with deleting them both.

Regarding "X*", I think I now understand that the "X*" (or "=X*") only works in formulas that take 'criteria', not in a general comparison like
=if(A3="X*",...,..)

2016-09-23 19:31:16

Peter

Lars

=SUMIF(A3:A7,"=X*",B3:B7)

=SUMPRODUCT(--(LEFT(A3:A7)="X")*(B3:B7))

HTH

2016-09-22 03:24:02

Lars Damgaard

the formula =SUMPRODUCT((LEFT(B\$1:AK\$1,1)="X")*B2:AK212)
is really nice and compressed. It also works with SUM instead of SUMPRODUCT
=SUM((LEFT(B\$1:AK\$1,1)="X")*B2:AK212)
but then you have to hit Ctrl-Shift-Enter (array formula). I guess the
(LEFT(B\$1:AK\$1,1)="X")*B2:AK212
part already made the multiplication to create in memory a 2D array with zeroes where the item name starts with X, so it suffices to sum this array, but SUMPRODUCT does the same when given a single 2D array as variable.

But can anyone explain when the "X*" trick in the SUMIF solution can be used? I could not make it workk in the SUMPRODUCT solution:
=SUMPRODUCT((B\$1:AK\$1="X*")*B2:AK212)

2016-09-17 14:32:47

Servi

I did not know about this SUMPRODUCT function, but it looks interesting and I will certainly explore it. I use SUMIF already much for adding financial data, according to cost codes. Recently I came accross the SUMIFS function (syntax: SUMIFS(sum_range, criteria_range1, criteria1, [criteria_range2, criteria2], ...)). I have made named cell ranges for the columns with the sum_range and for the criteria_ranges and this makes the formulas relatively easy. I can really recommend this. I use now one column with dates that set the reporting period and one column for cost codes.

2016-09-17 14:09:37

Brian Lair

I tested this to learn more & your formula SUMPRODUCT((LEFT(B\$1:AK\$1,1)="X")*B2:AK212) worked for me as-is. Very clever -- I didn't know about SUMPRODUCT -- thanks! Even with your explanation, though, it's still a bit mysterious. I guess I should practice using it in real life!

2016-09-17 11:59:48

allen@sharonparq.com

Steve: I tested out the one in the article (with the asterisk) and it worked fine.

-Allen

2016-09-17 10:07:33

steve

Allen, I believe the first formula should read
=SUMPRODUCT(--(LEFT(B1:AK1,1)=""),B2:AK2) & not * between the 2 Sumproduct arguments.

2016-09-17 05:41:19

SK SHARMA

Thanks Allen. You have a wonderful new ideas and professional development about excel.

Plz keep it up.

##### 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.

##### 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.)