简体   繁体   English

Excel:如果相关键在使用公式的值范围内,则对单元格区域求和

[英]Excel: Sum a range of cells if a related key is in a range of values using a formula

I'm trying to return the sum of a range of cells if an associated value is in a range of values used as filtering conditions. 如果关联值在用作过滤条件的值范围内,我试图返回一系列单元格的总和。 For example, using the following data table: 例如,使用以下数据表:

  Keys      Values
   A        500
   B        300
   C        600
   D         20
   A        150
   C        600

and the following list of keys to be summed: 以及以下要汇总的键列表:

Sum Keys:
   A
   D

I'm looking for a formulaic way to return 500+20+150 = 670. 我正在寻找一种公式化的方式来返回500 + 20 + 150 = 670。

I know you can use multiple SUMIFs, but the number of keys will change based on what the user wants to filter on, and writing enough SUMIFs to cover the max realistic cases (like 10ish) seems excessive, and I feel like there must be an easier way. 我知道你可以使用多个SUMIF,但是键的数量会根据用户想要过滤的内容而改变,并且编写足够的SUMIF来覆盖最大的实际情况(如10ish)似乎过多,我觉得必须有一个更简单的方法。

In my head, the formula would look something like this: 在我看来,公式看起来像这样:

=SUMIFS(Values, Keys, OR(Sum Keys), ...(optional other conditions))

I realize that this is an easy VBA problem, but I'm trying to avoid that because the spreadsheet will be audited by people with varying (or no) VBA knowledge. 我意识到这是一个简单的VBA问题,但我试图避免这种情况,因为电子表格将由具有不同(或没有)VBA知识的人员进行审核。 Also, adding an indicator column to the data to show if the key is in the Sum Keys range isn't realistic because there will be multiple tabs summarizing the data, each with their own filters on Key 此外,向数据添加一个指示符列以显示该键是否在Sum Keys范围内是不现实的,因为将有多个标签汇总数据,每个标签都有自己的Key过滤器

Any help is appreciated, though I realize this may be impossible without VBA. 感谢任何帮助,尽管我意识到没有VBA可能无法实现。 Please let me know if more information is needed. 如果需要更多信息,请告诉我。

Thanks! 谢谢!

EDIT: Thanks barry houdini for the answer! 编辑:谢谢巴里houdini的答案! See the comments of his post for the general solution for summing based on multiple ranges of acceptable criteria. 根据可接受标准的多个范围,查看他的帖子中关于求和的一般解决方案的评论。

You can use a formula like this 您可以使用这样的公式

=SUMPRODUCT(SUMIFS(Values,Key,Sum_Keys))

If Sum_Keys has 4 values then the SUMIFS formula returns a 4 value "array" (one for each Key in Sum_Keys) so you then need another function to sum that array. 如果Sum_Keys有4个值,则SUMIFS公式返回一个4值“数组”(Sum_Keys中每个键一个),因此您需要另一个函数来对该数组求和。 I use SUMPRODUCT because it avoids the need for "array entry". 我使用SUMPRODUCT是因为它避免了“数组输入”的需要。

You can add more conditions in the usual way, eg 您可以按常规方式添加更多条件,例如

=SUMPRODUCT(SUMIFS(Values,Key,Sum_Keys,Dates,Specifc_date))

If you want to sum items in accordance to filter conditions, consider using the 如果要根据过滤条件对项目求和,请考虑使用

=SUBTOTAL() = SUBTOTAL()

worksheet function. 工作表功能。 It is the tool to handle this task. 它是处理此任务的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM