简体   繁体   中英

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

=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. Either an excel solution or vba is fine

With the appropriate version of excel you can use the FORMULATEXT function to get the string comprising the formula.

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. The below formula is used to remove the "=SUMIF(" & ")"

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

The FIND functions find the position of the commas in the formula. The last LEN function is required to get the last criteria.

MID command is used to get the text between 2 commas. the "+1" and "-1" in the MID function are to remove the commas from the output. Couldn't generate a google sheet as the FORMULATEXT function was not recognised.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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