简体   繁体   English

Excel公式返回符合条件的范围,然后COUNTIF结果

[英]Excel formula to return a range matching a criteria, then COUNTIF the results

I've been struggling with this all day. 我一整天都在苦苦挣扎。 I had the following sheet that worked fine. 我有以下表格工作正常。 The user enters SUBJECT STATE, in this case KY for Kentucky. 用户输入SUBJECT STATE,在这种情况下为肯塔基州的KY。 Below there is a table called, "FullCompDataSet", with 3 columns. 下面有一个名为“FullCompDataSet”的表,有3列。 I need to do 2 things from this data ( output shown below highlighted in yellow ): 我需要从这些数据中做两件事( 下面显示的输出以黄色突出显示 ):

A. Count the number of records in FullCompDataSet whose "ST/Prov" match the SUBJECT STATE. A.计算FullCompDataSet中“ST / Prov”与SUBJECT STATE匹配的记录数。 I had this formula: 我有这个公式:

=COUNTIF(FullCompDataSet[ST/Prov],SubjectStateAbbreviation)

B. Return the Key for each record. B.返回每条记录的密钥。 I had this formula: 我有这个公式:

{=SMALL(IF(FullCompDataSet[ST/Prov]=SubjectStateAbbreviation,FullCompDataSet[Key]),ROW()-18)}

All of that worked fine, but now instead of simply matching the Subject State to the St/Prov, I have a list of compatible states I need to match ( example list at bottom highlighted in teal ). 所有这一切都运行良好,但现在不是简单地将主题状态与St / Prov匹配,而是我需要匹配的兼容状态列表(底部的示例列表以teal突出显示 )。 So, the new output I need is: 所以,我需要的新输出是:

A. 6 (because the subject state KY matches records in states of KY, IN, and TN) A. 6(因为主题国家KY匹配KY,IN和TN州的记录)

B. 413, 420, 434, 418, 404, 410 B. 413,420,434,418,404,410

Can anyone suggest the new formulas I'd need to use for A and B?? 谁能建议我需要用于A和B的新公式? I've tried all sorts of combinations of VLOOKUP/INDEX/MATCH, OFFSET, INDIRECT but haven't been able to get it. 我已经尝试过VLOOKUP / INDEX / MATCH,OFFSET,INDIRECT的各种组合但却无法获得它。 My apologies if there was a better way to post this issue, I'm still learning to post. 如果有更好的方式发布这个问题我很抱歉,我还在学习发帖。 THANK YOU! 谢谢!

FullCompDataSet Table FullCompDataSet表

Many-To-Many State Compatibility Map 多对多状态兼容性映射

Outside of using VBA I would use the following method; 在使用VBA之外我会使用以下方法; it's essentially a different in-sheet algo and or set of functions to the ones you're using. 它本质上是一个不同的表格算法或一组功能,你正在使用它们。 It will cater for changing dimensions of requested matches and as you see it also has adjustable output set. 它将满足更改所请求匹配的尺寸,如您所见,它还具有可调节的输出设置。 You'll essentially need to carry out 7 distinct function sets (easier than it sounds). 你基本上需要执行7个不同的功能集(比听起来更容易)。 I've included them in the picture; 我把它们包含在图片中; each color set uses the same function as shown in the comment. 每个颜色集使用与注释中显示的相同的功能。 The algo works very well and uses nothing more complicated than a vlookup. 算法非常好用,并没有比vlookup更复杂。 I've used it on much larger and more complex datasets. 我在更大更复杂的数据集上使用它。 You can also adapt it to function like a traditional Dictionary by assigning multiple key/identity sets. 您还可以通过分配多个键/标识集来使其适应传统词典的功能。 在此输入图像描述

If you have the latest version of office 365 you can use the following two formula to get what you want: 如果您拥有最新版本的office 365,则可以使用以下两个公式来获得所需内容:

For the count: 为了计数:

=SUM(IF(ISNUMBER(SEARCH(FullCompDataSet[ST/Prov],TEXTJOIN(" ",TRUE,IF(CompatState[State]=SubjectStateAbbreviation,CompatState[Compatible States],"")))),1,0))

For the SMALL: 对于小:

=SMALL(IF(ISNUMBER(SEARCH(FullCompDataSet[ST/Prov],TEXTJOIN(" ",TRUE,IF(CompatState[State]=SubjectStateAbbreviation,CompatState[Compatible States],"")))),FullCompDataSet[Key]),ROW(1:1))

Both are array formulas and must be confirmed with Ctrl-Shift-Enter on exiting edit mode. 两者都是数组公式,必须在退出编辑模式时使用Ctrl-Shift-Enter确认。 If done correctly Excel will put {} around the formulas. 如果操作正确,Excel会在公式周围放置{}

The TEXTJOIN formula was introduced in OFFICE 365 in February 2016. It makes a single string of the states that are compatible. TEXTJOIN公式于2016年2月在OFFICE 365中引入。它生成一串兼容的状态字符串。 It is this string that the SEARCH tries to find a match. SEARCH尝试查找匹配项是此字符串。 When it finds a match it is passed to the outside formulas. 当它找到匹配时,它将传递给外部公式。 One to count and one to find the correct Key. 一个计数,一个找到正确的密钥。

在此输入图像描述

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

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