简体   繁体   English

EXCEL:从sumif公式获取条件

[英]EXCEL: get criteria from a sumif formula

I've been trying to find a way to get the criteria from a sumif formula that looks like this 我一直试图找到一种方法来从看起来像这样的sumif公式中获取标准

=SUMIFS(N:N,G:G,"1670",H:H,"2016")

Every sumif has been hardcoded.... so I'm wanting to just pull out criteria1 in a cell (1670) and criteria2 (2016) in a cell. 每个sumif都经过硬编码....所以我想只提取一个单元格(1670)中的criteria1和一个单元格中的criteria2(2016)。 Either an excel solution or vba is fine excel解决方案或vba都可以

With the appropriate version of excel you can use the FORMULATEXT function to get the string comprising the formula. 使用适当版本的excel,您可以使用FORMULATEXT函数获取包含公式的字符串。

To get the criteria from this you have to make certain assumptions about the formatting of the formula - if you know commas occur only between argument it is quite easy to split the text on commas and extract; 要从中获取标准,您必须对公式的格式进行某些假设-如果您知道逗号仅在参数之间出现,则很容易将逗号分隔并提取文本; but in different locales you may find that a semicolon is used where you expect a comma; 但是在不同的语言环境中,您可能会发现在期望逗号的地方使用了分号; it is possible that ranges have commas in them; 范围中可能包含逗号; etc. 等等

How robust do you need the solution to be? 您需要解决方案的坚固程度如何?

Extending the answer from @Floris, the below set of formulas gives your the criteria range and criteria in the formula. 从@Floris扩展答案,下面的公式集为您提供了标准范围和公式中的标准。 The below formula is used to remove the "=SUMIF(" & ")" 下面的公式用于删除“ = SUMIF(”&“)”

=SUBSTITUTE(SUBSTITUTE(FORMULATEXT(SUMIF_FORMULA),"=SUMIFS(",""),")","")

The FIND functions find the position of the commas in the formula. FIND函数可查找公式中逗号的位置。 The last LEN function is required to get the last criteria. 需要最后一个LEN函数才能获取最后一个条件。

MID command is used to get the text between 2 commas. MID命令用于获取两个逗号之间的文本。 the "+1" and "-1" in the MID function are to remove the commas from the output. MID函数中的“ +1”和“ -1”用于从输出中删除逗号。 Couldn't generate a google sheet as the FORMULATEXT function was not recognised. 无法识别FORMULATEXT函数,因此无法生成Google工作表。

在此处输入图片说明

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

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