问题
I'm trying to find a way to use sumproduct in excel but for only the records that begin with a certain character string.
Basic sumproduct
=SUMPRODUCT(BC:BC,BD:BD)/SUM(BC:BC)
Is there any way to make each range dependent upon a search criteria? So let's say, only use records in BC:BC & BD:BD that have a corresponding record in BA:BA that begins with Stack. I don't think you can use wildcards in sumproduct but is there another function I could be using?
Thanks.
回答1:
You CAN do this using sumproduct... The trick is to use the -- operator to change True / False to 1 / 0 and then you can multiply your other columns by that vector so as to get only the rows where your statement evaluates to true
Try this:
=SUMPRODUCT(--(ISNUMBER(FIND("Stack",A:A))),BC:BC,BD:BD)/SUM(BC:BC)
To explain, the part --(ISNUMBER(FIND("Stack",A:A))) basically says If the word "Stack" exists in the cell in column A, evaluate to 1, otherwise, evaluate to 0 - Then you multiply by the values in BC & BD to get your final result.
One note:
You could make this formula more efficient / faster to calculate (if you're using it A LOT in your sheet, by replacing, for example, BC:BC to include the maximum number of rows you want, so, for example BC:BC becomes BC1:BC1000 - Therefore not having to try and calculate it for the entire column.
Hope this helps!!
回答2:
For "begins with stack" specifically you can use LEFT function, e.g.
=SUMPRODUCT((LEFT(BA:BA,5)="stack")+0,BC:BC,BD:BD)/SUMIF(BA:BA,"stack*"BC:BC)
where 5 corresponds to the length of the search term "stack"
As John Bustos says, better to restrict your ranges for SUMPRODUCT - using the whole column may be slow
来源:https://stackoverflow.com/questions/22353753/using-a-wildcard-in-a-sumproduct-in-excel