计算 Excel 电子表格中满足特定条件的唯一行数
我想计算 Excel 列中包含特定字符串的所有唯一值。例如,假设我的专栏中有这个:
strA
strB
斯特拉
字符串
差异
差异B
strC
如果我给出这个理论函数,我正在写一个值“str”,它将计算包含“str”的唯一值,并返回数字 3。
有什么建议吗?网络上对这个问题充满了半答案,但我花了一个小时试图找到一个明确的解决方案,所以我认为 StackOverflow 可能会从这个问题中受益。
I want to count all the unique values in an Excel column that contain a certain string. For instance, say I have this in my column:
strA
strB
strA
strC
diffA
diffB
strC
If I give this theoretical function that I'm writing a value of "str," it will count the unique values containing "str", and give me back the number 3.
Any suggestions? The web is full of half-answers to this problem, but I've spent an hour trying to find a clear solution, so I thought StackOverflow might benefit from the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当使用输入样本运行时,结果为 3。循环的终止条件是在列中找到第一个空单元格。如果逻辑不是你想要的,你可以调整它。
您需要在 VBA 部分添加对 Microsoft Scripting Runtime 的引用才能编译和运行该宏。
When run with your input sample, this gives 3. The terminating condition for the loop is the first empty cell found in a column. You can adjust that if the logic isn't what you want.
You need to add a reference to Microsoft Scripting Runtime in the VBA section for this macro to compile and run.
使用 Control+Shift+Enter 输入,而不仅仅是 Enter,因为它是数组公式。通常,要计算列表中唯一项目的数量,您可以输入
I 将 COUNTIF 部分乘以另一个条件(以 str 开头),以便那些不以字符串开头的项目被视为零。如果您不熟悉数组公式,请参阅
http://www.dailydoseofexcel.com/archives/2004/04/05/anatomy-of-an-array-formula/
Enter with Control+Shift+Enter, not just Enter, because it's an array formula. Typically to count the number of unique items in the list, you would array enter
I multiplied the COUNTIF portion by another criterion (begins with str) so that those that don't begin with the string are treated as zero. If you're new to array formulas, see
http://www.dailydoseofexcel.com/archives/2004/04/05/anatomy-of-an-array-formula/
我太慢了,但我得到了不同的解决方案:
将要搜索的字符串放入 B1 中,然后输入数组公式:
I have been too slow, but I got a different solution:
Put the string to search in B1 then Enter as an Array Formula: