Вы находитесь на странице: 1из 80

SUMPRODUCT

Introduction
My friend Bill tells me that he feels like a REBORN EXCEL USER since he has discovered the SUMPRODUCT formulas. Here are 136 of them to show you why. Data queried or downloaded from a data warehouse (Oracle, Sybase, SQL Server, etc.), a datamart (Access) or a major application (SAP, JDEdwards, PeopleSoft, etc.) look like the data in the "Data" sheet of this workbook. What you want is a report like the ones in the "Report" sheet of this workbook. Use the SUMPRODUCT formula. Your data can be on the same sheet as the SUMPRODUCT formula, in the same workbook or even in another workbook Often the number of lines (records) varies and the things are not on the same line as in the last download. With ordinary formulas, you cannot sub-total amounts or quantities based on one or more criteria. SUMPRODUCT formulas take care of all that. In the sheet "Formula" of this workbook, I explain to you how to build a SUMPRODUCT formula. Read carefully the note below to avoid problems.

Notes
The rows or columns used as references in a SUMPRODUCT formula must have the same number of cells, Ex : SUMPRODUCT((A1:A6="January")*(B1:B6)) NOT SUM ((A1:B6=January")*(A1:B7)). This is important to remember specially when you use named fields. Never include cells containing text in the ranges that you sum within SUMPRODUCT formulas you will end up with a "#VALUE" error message. So, no title cells. When you import data from certain old systems the cell with no data is not really empty. It might contain spaces and SUMPRODUCT formulas will result in a "#VALUE" error message. Remove these spaces by using the replace function where you enter a space in the to window and nothing in the "Replace by". Another situation tha can happen with data coming from an old system is that there can be spaces at the end of a string of character. For example, instead of having "Chicago" in a cell, you have "Chicago " because there are 15 spaces to enter the name of the city in the database. A SUMPRODUCT formula looking at this cell will not find "Chicago". You need to use the procedure mentionned in the previous paragraph to remode the spaces.

Your comments on this "Resource Workbook" would be appreciated: Click here: workbooks@excel-vba.com

Pierre (Peter) Leclerc

(613) 749-4695

UCT

since he has discovered the

se, SQL Server, etc.), a datamart (Access) e the data in the "Data" sheet of this sheet of this workbook. Use the he SUMPRODUCT formula, in the same

n the same line as in the last download. based on one or more criteria.

ld a SUMPRODUCT formula. Read

mula must have the same number of cells, B6=January")*(A1:B7)). This is important

n SUMPRODUCT formulas you will end

a is not really empty. It might contain or message. Remove these spaces by w and nothing in the "Replace by".

tem is that there can be spaces at the end n a cell, you have "Chicago " because A SUMPRODUCT formula looking at this ned in the previous paragraph to remode

" would be appreciated: ba.com

The SUMPRODUCT formula


Sums with SUMPRODUCT
The basic SUMPRODUCT formula to sum quantities or amounts looks like this: =SUMPRODUCT((B2:B500="Bikes")*(C2:C500="Montreal")*(E2:E500)) In plain English, it means: sum cells E2 to E500 if the value in the corresponding cell in column "C" is "Montreal" and the value in the corresponding cell in column "B" is "Bikes" The column to be summed is always the last (in red above). All arguments (components of a formula (in blue above)) are separated by an asterisk. In the formula above there are two criteria but there could be only one or as many as needed. If you are submitting a text as criteria ("Bikes" and "Montreal" in the example above) you must use double quotes. If your criteria are numbers you don't need to use the double quotes. When you import data from certain databases, the numbers can be considered as text and you might need to use double quotes with numbers also. If you use addresses as criteria, you don't need double quotes either (like in the example below). In this example, we assume that the value of cell "K1" is "Montreal" and the value of cell "K2" is "Bikes". =SUMPRODUCT((B2:B500=K1)*(C2:C500=K2)*(E2:E500)) Usually, the data set is on one sheet and the reports are on different sheets. The formula will then look like the following assuming that your data set is on sheet "Data1": =SUMPRODUCT((Data1!B2:B500=K1)*(Data1!C2:C500=K2)*(Data1!E2:E500)) You don't really need to key in the name of the sheet. Just enter "=SUMPRODUCT(" and then do not click on enter, just go to your data sheet and select the range, Excel will write the address for you. Notice that if the name of the sheet is not a single word, you will see apostrophes appear like in ('Data 1'!B2:B500=K1). The apostrophe is there to tell Excel that both "Data" and "1" are part of the name of the sheet.

Counts with SUMPRODUCT


The basic SUMPRODUCT formula to count looks like this: =SUMPRODUCT((B2:B500="Bikes")*(B2:B500="Bikes")) Notice that both argument are the same. In plain English this formula says: count the number of records (lines in database language) where the product "Bikes" appear in column "B". When counting contrary to when summing the cells do not have to carry a number. The next formula says: count the number of records (lines in database language) where the product "Bikes" appear in column "B" and where "Montreal appears in column "C". =SUMPRODUCT((C2:C500="Montreal")*(B2:B500="Bikes")*(B2:B500="Bikes"))

Looking with SUMPRODUCT

There are other situations where SUMPRODUCT can be useful. For example imagine that you have a table of auto insurance rates with 5 columns for the sex, the age, the city, the type of car and the rate. You are looking for a rate, you do not really want to sum anything, you just want a single rate. The SUMPRODUCT formula will indeed sum colum "E" but as it will find a single rate based on the criterias it will give you your answer. Here what it would look like: =SUMPRODUCT((Data1!A2:A500="M")*(Data1!B2:B500=30)*(Data1!C2:C500="Chicago")*(Data1!D2:D500="Sedan")*(Data1! Copy/Paste SUMPRODUCT

This last section will probably sound very complex and confusing to begin with but I strongly recommend that you practice and become good at using the $ in addresses. It will save you a lot of time and keyboard work. Practice and, all of a sudden it will become clear. I apologize if my explanation sounds complex but it is this kind of thing that only becomes clear once you have practiced a few times. Like me you don't want to have to key in 100 formulas in a 10 X 10 table, you want to write a single formula in the top left cell of the table and copy/paste it all around. To do so, you need to master the art of absolute and relative addresses. You have to use the dollar signs to achieve the task because "B10", "$B10", "B$10" and "$B$10" do not react the same to copy/paste. First a MAJOR tip: you don't need to key in the dollar signs "$". You go to the formula bar (the window whre you enter text and numbers above the sheet), you click on an address and then you click on "F4" once ($B$10), twice (B$10), three times ($B10) and then a forth time to go back to (B10). I ALWAYS use "$" in ALL my formulas even the ones that I don't copy/paste to develop a reflex and not have to think how to do it each time I really need the THING. I ALWAYS use "$" in ALL my formulas because if I don't I will have to write hundreds of formulas instead of a single one. Once you are used to adding this thing "$" it gets very simple. If you copy/paste a formula with $B$10 right, left, up or down, "$B$10! will remain "$B$10" If you copy/paste a formula with B10 right or left the column "B" will change but the line "10" won't, if you copy it up or down, the "B" will remain but the "10" will change. If you copy/paste it diagonally, both "B" and "10" will change. If you copy/paste a formula with $B10 right or left the column "B" will NOT change and the line "10" won't change either, if you copy it up or down, the "B" will remain and the "10" will change. If you copy/paste it diagonally, "B" will remain but "10" will change. If you copy/paste a formula with B$10 right or left the column "B" will change but the line "10" won't, if you copy it up or down, the "B" will remain and so will the "10". If you copy/paste it diagonally, "B"will change but not the "10". Lets see the formula in cell B12 of the sheet "Report" of this workbook: =SUMPRODUCT((Data1!$B$2:$B$500=$A12)*(Data1!$C$2:$C$500=B$11)*(Data1!$E$2:$E$500)) All adresses for the database have two $ because wherever I copy/paste this formula, I want it to ALWAYS look at the same set of cells. This set up applies in 90% of the cases where your SUMPRODUCT formula looks at a database. Let's look at $A12 now. This refers to the title cell of the row so when I copy it laterally, I want it to still look at column "A" but Iwhen I copy it up/down I want the row number to change. As for the B$11 it refers to the title cell of the column so when I copy it laterally, I want the column address to change yet I want the row number to remain the same.en I copy it up/down I want the row number to change. In some cases when you look at a data set that carries monthly data in twelve different columns, the column that you sum can change so you will not put a $ in front of the column but you will keep the one in front of the row because wherever the formula is copy/pasted, you want to sum the same rows. In the formula above, you would use E$2:E$500 instead of $E$2:$E500$. Don't forget the notes in the Introduction about why a SUMPRODUCT results in an error. Enjoy and Good Luck

umn "C" is "Montreal" and the a formula (in blue above)) are

ed.

ust use double quotes. If your certain databases, the numbers

elow). In this example, we assume

will then look like the following

hen do not click on enter, just go to name of the sheet is not a single ere to tell Excel that both "Data"

ber of records (lines in database en summing the cells do not have

e product "Bikes" appear in column

you have a table of auto insurance king for a rate, you do not really sum colum "E" but as it will find a

)*(Data1!D2:D500="Sedan")*(Data1!E2:E500))

y recommend that you practice ork. Practice and, all of a sudden it hat only becomes clear once you a single formula in the top left cell

he dollar signs to achieve the task

the window whre you enter text B$10), twice (B$10), three times

eflex and not have to think how to I don't I will have to write hundreds simple.

" won't, if you copy it up or down, " will change.

line "10" won't change either, if you ally, "B" will remain but "10" will

0" won't, if you copy it up or down, ot the "10".

nt it to ALWAYS look at the same oks at a database.

ant it to still look at column "A" but

column address to change yet I ange.

umns, the column that you sum of the row because wherever the use E$2:E$500 instead of

Month April April April April April April April April April April April April April April April April April April April April February February February February February February February February February February February February February February February February February February February February January January January January January January January January January January January January January January January

Product Bikes Bikes Bikes Bikes Skates Skates Skates Skates Skis Long Skis Long Skis Long Skis Long Skis Short Skis Short Skis Short Skis Short Snow Board Snow Board Snow Board Snow Board Bikes Bikes Bikes Bikes Skates Skates Skates Skates Skis Long Skis Long Skis Long Skis Long Skis Short Skis Short Skis Short Skis Short Snow Board Snow Board Snow Board Snow Board Bikes Bikes Bikes Bikes Skates Skates Skates Skates Skis Long Skis Long Skis Long Skis Long Skis Short Skis Short Skis Short

Store Quantity Amount Montreal 12 4500 Montreal 56 21000 San Francisco 854 320250 New York 25 9375 Montreal 56 5544 Toronto 854 84546 San Francisco 25 2475 New York 663 65637 Montreal 854 209230 Toronto 25 6125 San Francisco 663 162435 New York 21 5145 Montreal 21 4389 Toronto 4 836 San Francisco 522 109098 New York 136 28424 Montreal 4 1232 Toronto 522 160776 San Francisco 125 38500 New York 663 204204 Montreal 663 248625 San Francisco 21 7875 San Francisco 54 20250 New York 658 246750 Montreal 21 2079 Toronto 663 65637 San Francisco 21 2079 New York 136 13464 Montreal 522 127890 Toronto 125 30625 San Francisco 663 162435 New York 21 5145 Montreal 21 4389 Toronto 4 836 San Francisco 522 109098 New York 125 26125 Montreal 21 6468 Toronto 136 41888 San Francisco 21 6468 New York 4 1232 Montreal 75 28125 Toronto 12 4500 San Francisco 136 51000 New York 21 7875 Montreal 21 2079 New York 4 396 San Francisco 522 51678 New York 212 20988 Montreal 136 33320 Toronto 21 5145 San Francisco 4 980 New York 522 127890 Montreal 663 138567 Toronto 21 4389 San Francisco 136 28424

Add data down to line 500, move them around, sort them by city, by month, by product, by quantity, by amount and your reports still hold. Delete some rows, move them around and your reports still holds.
Modify the formulas in the report (change the 500 for 2000) and sum data up to line 1,000, or 2,000.

January January January January January March March March March March March March March March March March March March March March March March March March March

Skis Short Snow Board Snow Board Snow Board Snow Board Bikes Bikes Bikes Bikes Skates Skates Skates Skates Skis Long Skis Long Skis Long Skis Long Skis Short Skis Short Skis Short Skis Short Snow Board Snow Board Snow Board Snow Board

New York Montreal Toronto San Francisco New York Montreal Montreal San Francisco New York Montreal Toronto San Francisco New York Montreal Toronto San Francisco New York Montreal Toronto San Francisco San Francisco Montreal Toronto San Francisco New York

21 522 663 21 136 4 522 125 212 56 854 212 56 854 663 21 136 4 522 136 21 21 4 522 125

4389 160776 204204 6468 41888 1500 195750 46875 79500 5544 84546 20988 5544 209230 162435 5145 33320 836 109098 28424 4389 6468 1232 160776 38500

ta down to line 500, move them around, sort y city, by month, by product, by quantity, by and your reports still hold. Delete some rows, hem around and your reports still holds.

the formulas in the report (change the 500 for nd sum data up to line 1,000, or 2,000.

SUMPRODUCT Formulas
Modify the data in the "Data Sheet" and see the tables and charts change.
SUMMING Bikes Skates Skis Long Skis Short Snow Board Total COUNTING Bikes Skates Skis Long Skis Short Snow Board Total

Back to Introduction

Sales by Store ($)


Montreal 499,500 $ 15,246 $ 579,670 $ 148,181 $ 174,944 $ 1,417,541 $ Toronto San Francisco 4,500 $ 446,250 $ 234,729 $ 77,220 $ 204,330 $ 330,995 $ 115,159 $ 279,433 $ 408,100 $ 212,212 $ 966,818 $ 1,346,110 $ New York 343,500 $ 106,029 $ 171,500 $ 58,938 $ 285,824 $ 965,791 $ Total 1,293,750 $ 433,224 $ 1,286,495 $ 601,711 $ 1,081,080 $ 4,696,260 $
800,000 $ 600,000 $ 400,000 $ 200,000 $ 0$

Number of stores selling in March


Montreal 2 1 1 1 1 6 Toronto 0 1 1 1 1 4 San Francisco 1 1 1 2 1 6 New York 1 1 1 0 1 4 Total 4 4 4 4 4 20

In most of these tables, I have wr one SUMPRODUCT formula in th cell and copied it to the rest of the To do so, one must use wisely ab relative references ex.(B10, $B10 or $B$10 ??).

Sales per Month ($)


Bikes Skates Skis Long Skis Short Snow Board Total January February 91,500 $ 523,500 $ 75,141 $ 83,259 $ 167,335 $ 326,095 $ 175,769 $ 140,448 $ 413,336 $ 56,056 $ 923,081 $ 1,129,358 $ March 323,625 $ 116,622 $ 410,130 $ 142,747 $ 206,976 $ 1,200,100 $ April 355,125 $ 158,202 $ 382,935 $ 142,747 $ 404,712 $ 1,443,721 $ Total 1,293,750 $ 433,224 $ 1,286,495 $ 601,711 $ 1,081,080 $ 4,696,260 $
600,000 $ 500,000 $ 400,000 $ 300,000 $ 200,000 $ 100,000 $ 0$

Sales by Store (Qty)


Bikes Skates Skis Long Skis Short Snow Board Total Montreal 1,332 154 2,366 709 568 5,129 Toronto San Francisco 12 1,190 2,371 780 834 1,351 551 1,337 1,325 689 5,093 5,347 New York 916 1,071 700 282 928 3,897 Total 3,450 4,376 5,251 2,879 3,510 19,466
2,500

2,000 1,500
1,000 500

0
Bikes

Sales per Month (Qty)


Bikes Skates Skis Long Skis Short Snow Board Total January 244 759 683 841 1,342 3,869 February 1,396 841 1,331 672 182 4,422 March 863 1,178 1,674 683 672 5,070 April 947 1,598 1,563 683 1,314 6,105 Total 3,450 4,376 5,251 2,879 3,510 19,466
2,000 1,500 1,000 500 0 Bikes

Ski Sales by Store ($)


Skis Montreal 727,851 $ Toronto San Francisco 319,489 $ 610,428 $ New York Total 230,438 $ 1,888,206 $

Sales per Month (Canada) (Qty)


January Bikes Skates Skis Long Skis Short Snow Board Total 87 21 157 684 1185 2134 February 663 684 647 25 157 2176 March 526 910 1517 526 25 3504 April 68 910 879 25 526 2408 Total 1344 2525 3200 1260 1893 10222
2000 1500 1000 500 0 Bikes

Sales per Month by Store (Qty)


Montreal Toronto New York San Francisco Total January 1417 717 916 819 3869 February 1248 928 944 1302 4422 March 1461 2043 529 1037 5070 April 1003 1405 1508 2189 6105 Total 5129 5093 3897 5347 19466

2500 2000 1500 1000 500 0


January

Sales per Month by Store ($)


Montreal Toronto New York San Francisco Total January February 362,867 $ 389,451 $ 218,238 $ 138,986 $ 203,426 $ 292,716 $ 138,550 $ 308,205 $ 923,081 $ 1,129,358 $ March 419,328 $ 357,311 $ 156,864 $ 266,597 $ 1,200,100 $ April Total 245,895 $ 1,417,541 $ 252,283 $ 966,818 $ 312,785 $ 965,791 $ 632,758 $ 1,346,110 $ 1,443,721 $ 4,696,260 $

800,000 $ 600,000 $ 400,000 $


200,000 $

0$

Sales per Month by Country ($)


Canada United States Total January February 581,105 $ 528,437 $ 341,976 $ 600,921 $ 923,081 $ 1,129,358 $ March 776,639 $ 423,461 $ 1,200,100 $ April Total 498,178 $ 2,384,359 $ 945,543 $ 2,311,901 $ 1,443,721 $ 4,696,260 $

1,000,000 $

800,000 $
600,000 $

400,000 $
200,000 $ 0$

Back to Introduction

800,000 $ 600,000 $ 400,000 $ 200,000 $ 0$ Bikes Skates Skis Long Skis Short Snow Board

Montreal
Toronto

San Francisco New York

In most of these tables, I have written only one SUMPRODUCT formula in the top left cell and copied it to the rest of the table. To do so, one must use wisely absolute or relative references ex.(B10, $B10, B$10 or $B$10 ??).

600,000 $ 500,000 $ 400,000 $ 300,000 $ 200,000 $ 100,000 $ 0$

January February March

Bikes

Skates

Skis Long Skis Short

Snow Board

April

Montreal Toronto San Francisco

New York

Bikes

Skates

Skis Long

Skis Short

Snow Board

January February
March

Bikes

Skates

Skis Long Skis Short

Snow Board

April

January
February March

Bikes

Skates

Skis Long Skis Short

Snow Board

April

Montreal
Toronto New York San Francisco

January

February

March

April

800,000 $ 600,000 $ 400,000 $


200,000 $
Montreal

Toronto New York


San Francisco

0$ January February March April

Canada United States

January

February

March

April

Looking up with SUMPRODUCT


Example 1 In this example you are looking for a rate but there are three criterias and both LookUp and Index/Match can't be used. Here is how to do it with Choose a make, a year and an engine and see the rate appear.

Make Cadillac Cadillac Cadillac Cadillac Cadillac Cadillac Lincoln Lincoln Lincoln Lincoln Lincoln Lincoln

Year 2001 2002 2003 2001 2002 2003 2001 2002 2003 2001 2002 2003

Engine 8 cyl. 8 cyl. 8 cyl. 6 cyl. 6 cyl. 6 cyl. 8 cyl. 8 cyl. 8 cyl. 6 cyl. 6 cyl. 6 cyl.

Rate 0.04 0.05 0.06 0.07 0.08 0.09 0.14 0.15 0.16 0.17 0.18 0.19

Make Year Engine Rate

Cadillac 2003 6 cyl. 0.09

Example 2 In this example your are also looking for a rate based on a salary. State New York New York New York Oregon Oregon Oregon From To Rate $0 $50,000 0.05 $50,001 $100,000 0.1 $100,001 $5,000,000 0.15 $0 $50,000 0.16 $50,001 $100,000 0.19 $100,001 $5,000,000 0.22

State Salary Rate:

Oregon $102,000 0.22

ose a make, a year and ngine and see the rate

Вам также может понравиться