# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author 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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.