简体   繁体   English

excel 中包含合并单元格的 Countif

[英]Countif with Merged Cells in excel

I have the below table.我有下表。 From this, I would like to get the count of the subject each class has as shown in Column F using a function. Like as how many subjects we have in Class A, how many subjects we have in Class B, and so on.由此,我想使用 function 来计算每个 class 的主题数,如 F 列所示。就像我们在 Class A 中有多少个主题,在 Class B 中有多少个主题,等等。

I used the COUNTIF formula, but it didn't work because of the merged cells.我使用了 COUNTIF 公式,但由于单元格合并,它不起作用。

在此处输入图像描述

Is it possible to get the count of the subjects keeping the merged cells?是否有可能获得保留合并单元格的主题的数量?

One way of doing this could be through REDUCE() :一种方法是通过REDUCE()

在此处输入图像描述

Formula in D1 : D1中的公式:

=LET(a,SCAN(A2,A2:A21,LAMBDA(a,b,IF(b="",a,b))),REDUCE(A1:B1,UNIQUE(a),LAMBDA(b,c,VSTACK(b,HSTACK(c,SUM(--(a=c)))))))

Note that SCAN() acts like a filldown function. Also note that merged cells are evil!请注意SCAN()的作用类似于填充 function。还要注意合并的单元格是邪恶的!

=LET(a,A2:A21, 
     c,DROP(REDUCE(0,a,LAMBDA(x,y,VSTACK(x,IF(y="",TAKE(x,-1),y)))),1),
     u,UNIQUE(c),
     m,MMULT(--(TRANSPOSE(c)=u),SEQUENCE(COUNTA(c),,1,0)),
HSTACK(u,m))

Where c creates the unmerged equivalent of column A replacing blanks with the value above and m creates the count for each value that equals the unique values u in the unmerged version of column A.其中c创建 A 列的未合并等价物,用上面的值替换空白, m为每个值创建等于 A 列未合并版本中唯一值u的计数。

Edit: as Mayukh commented we could replace MMULT by MAP/SUM:编辑:正如 Mayukh 评论的那样,我们可以用 MAP/SUM 替换 MMULT:

=LET(a,A2:A21,
     c,DROP(REDUCE(0,a,LAMBDA(x,y,VSTACK(x,IF(y="",TAKE(x,-1),y)))),1),
     u,UNIQUE(c),
HSTACK(u,MAP(u,LAMBDA(x,SUM(--(c=x))))))

Here is another way using MAP()这是使用MAP()的另一种方法

在此处输入图像描述


• Formula used in cell D2 • 单元格D2中使用的公式

=LET(a,LOOKUP(ROW(A2:A21),ROW(A2:A21)/(A2:A21<>""),A2:A21),
b,UNIQUE(a),
c,MAP(b,LAMBDA(x,SUM(--(x=a)))),
VSTACK(A1:B1,HSTACK(b,c)))

We can use SCAN() as well, since JvdV Sir has already shown that option, hence using the OLDSCHOOL Function LOOKUP() .我们也可以使用SCAN() ,因为JvdV Sir 已经显示了该选项,因此使用OLDSCHOOL Function LOOKUP() Which simply does the same thing as shown using SCAN() refer the screenshot below,它只是做与使用SCAN()所示相同的事情,请参考下面的屏幕截图,

在此处输入图像描述

• Using SCAN() • 使用SCAN()

=SCAN(A2:A21,A2:A21,LAMBDA(x,y,IF(y="",x,y)))

• Using LOOKUP() • 使用LOOKUP()

=LOOKUP(ROW(A2:A21),ROW(A2:A21)/(A2:A21<>""),A2:A21)

Few friendly suggestions:几个友情建议:

  • Merged cells may help you arrange values in a meaningful way, but they come with problems;合并单元格可能会帮助您以有意义的方式排列值,但它们会带来问题;
  • You can't sort columns when using merged cells;使用合并单元格时不能对列进行排序;
  • You can't use the Filter option, even if you could some bigger problems waiting for you to make that work out, try that out;你不能使用 Filter 选项,即使你可能有一些更大的问题等着你去解决,试试看;
  • Instead use Center Across Selection which is a much better alternative to merging.而是使用Center Across Selection ,这是比合并更好的选择。
  • Last but not least, I have seen most experts suggests and recommends in avoiding merged cells.最后但并非最不重要的是,我看到大多数专家建议和建议避免合并单元格。 So please avoid merged cells.所以请避免合并单元格。

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

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