Question about Sumproduct Function

Hi Experts,

The function below attempts to use Trim function on a range name so that I can indent the referenced cell (the range has no indents). It fails as soon as I add a space to the name in A11.

In another case I tried to extract the year from a date by using the Year function in a similar way to what you see below. That also failed and I had to create a new column in the data.

=SUMPRODUCT(-(TRIM(tblSalariesName)=$A11),-(tblSalariesYear=B$3),(tblSalariesAmount))

I admit to being a Lotus 1-2-3 user when I can be wherein all of this is quite simple and elegant.

Thanks for any help and have a happy new year
rjfieldsAsked:
Who is Participating?
 
barry houdiniConnect With a Mentor Commented:
You would need to apply TRIM to A11 too, try like this

=SUMPRODUCT(-(TRIM(tblSalariesName)=TRIM($A11)),-(tblSalariesYear=B$3),tblSalariesAmount)

Note that TRIM will remove leading and trailing zeroes and also reduce repeated consecutive internal spaces to a single space, so that might affect your comparison

If you don't want to have a separate column for the year you should be able to use this version

=SUMPRODUCT(-(TRIM(tblSalariesName)=TRIM($A11)),-(YEAR(tblSalariesDate)=B$3),tblSalariesAmount)

....but that will fail if tblSalariesDate includes any text values (e.g. a header) because YEAR function applied to text gives #VALUE! error, an alternative is to use TEXT function which will be OK with headers, i.e.

=SUMPRODUCT(-(TRIM(tblSalariesName)=TRIM($A11)),-(TEXT(tblSalariesDate,"yyyy")=B$3&""),tblSalariesAmount)

regards, barry

PS using SUMPRODUCT with a single - in front of each condition is valid but it's more normal to double those to --

If you use a single - you need to make sure you have an even number of conditions otherwise your result might be negated (e.g. you get -2000 instead of 2000)
0
 
rjfieldsAuthor Commented:
Thank you very much.

It turns out I made a hugely lame mistake misplacing the Trim function. OY! I caught it looking at your example. Also, you are right about the unary operator. I had an even number of them otherwise one needs to use a double unary (--) operator.

12/27
I did try to cut and paste your suggestion for the Year function above after redefining the range to exclude the column header. I also shortened the range to 10 rows and verified that I had valid dates within it. I still get #value as a return complaining about a datatype error.

12/28

Indeed you were correct about the header in the range definition causing the #value error. I just needed to take the additional step of adjusting all the other relevant ranges to omit the header cell as well to preserve the symmetry of the query.

Again thanks much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.