在 Excel 中计算唯一值
我需要在 Excel 中计算范围 (C2:C2080) 中的唯一值。谷歌搜索公式:
=SUM(IF(FREQUENCY(MATCH(C2:C2080;C2:C2080;0);MATCH(C2:C280;C2:C2080;0))>0;1))
返回错误值。
UPD:蹩脚的解决方案:
Sub CountUnique()
Dim i, count, j As Integer
count = 1
For i = 1 To 470
flag = False
If count > 1 Then
For j = 1 To count
If Sheet1.Cells(i, 3).Value = Sheet1.Cells(j, 11).Value Then
flag = True
End If
Next j
Else
flag = False
End If
If flag = False Then
Sheet1.Cells(count, 11).Value = Sheet1.Cells(i, 3).Value
count = count + 1
End If
Next i
Sheet1.Cells(1, 15).Value = count
End Sub
I need to count unique values in range (C2:C2080) in excel. Googled formula:
=SUM(IF(FREQUENCY(MATCH(C2:C2080;C2:C2080;0);MATCH(C2:C280;C2:C2080;0))>0;1))
return incorrect value.
UPD: Lame solution:
Sub CountUnique()
Dim i, count, j As Integer
count = 1
For i = 1 To 470
flag = False
If count > 1 Then
For j = 1 To count
If Sheet1.Cells(i, 3).Value = Sheet1.Cells(j, 11).Value Then
flag = True
End If
Next j
Else
flag = False
End If
If flag = False Then
Sheet1.Cells(count, 11).Value = Sheet1.Cells(i, 3).Value
count = count + 1
End If
Next i
Sheet1.Cells(1, 15).Value = count
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是一个适合我的 VBA 函数。
您可以将其用作工作表函数< /a>,引用任何范围,例如“=CountUnique(N8:O9)”
它处理文本和数值,并将空白单元格视为一个值
它不需要处理数组函数。
对于字典对象,它需要引用 Microsoft 脚本库。
Here is a VBA function that works for me.
You can use it as a worksheet function, referencing any range, eg “=CountUnique(N8:O9)”
It handles text and numeric values, and treats blank cells as one value
It does not require dealing with array functions.
It requires a reference to the Microsoft Scripting Library, for the dictionary object.
http://office.microsoft.com/en-us/excel/HP030561181033.aspx
您还可以编写一个 VBA 宏(不过不确定这是否是您想要的。)
其效果如下(给定一个 A1-A11 已填充且 B1-B11 为空的电子表格):
http://office.microsoft.com/en-us/excel/HP030561181033.aspx
You may also write a VBA macro (not sure if that's what you're after though.)
Something to the effect of (given a spreadsheet with A1-A11 filled and B1-B11 empty):
尝试:
=SUM(IF(FREQUENCY(C2:C2080,C2:C2080)>0,1))
编辑:以上将会处理列中的空白条目
Try:
=SUM(IF(FREQUENCY(C2:C2080,C2:C2080)>0,1))
EDIT: The above will handle blank entries in the column
JustinG 的函数运行得非常好(而且速度很快),直到由于 Excel 中的某种类型的限制而导致唯一项目的数量超过 32,767。
我发现如果你修改他的代码
并将其设置为...
然后它将处理更多独特的项目。
JustinG's function works very well (and fast) until the number of unique items exceeds 32,767 due to some type of limit in Excel.
I found if you modify his code
and make it as...
It will then handle more unique items.
对于仍然尝试使用 @JustinG 的字典方法的任何人,如果您使用的是较新版本的 VBA,则需要稍微更改代码。
您需要引用“Microsoft Scripting Runtime”并在
Dictionary
术语前添加Scripting
,如下所示:For anyone still trying to use @JustinG's dictionary method, you will need to alter the code slightly if you're using a newer version of VBA.
You'll need to reference 'Microsoft Scripting Runtime' and prefix the
Dictionary
terms withScripting
, as follows:阅读本文并进一步调查后,我得到了一个比我在这里看到的任何东西都更适合我的方法:
Array-enter:
(Ctrl+Shift+Enter,并且不包括大括号)
或者在 VBA 中:
它适用于数字和文本,它处理空白单元格,它处理引用单元格中的错误,并且它在 VBA 中工作。它也是我见过的最紧凑的解决方案之一。在 VBA 中使用它,它显然会自动处理数组公式的需要。
请注意,它处理错误的方式是简单地将错误包含在唯一值计数中。例如,如果有两个单元格返回#DIV/0!以及三个返回#VALUE! 的单元格,这 5 个单元格会将 2 添加到唯一值的最终计数中。如果您希望完全排除错误,则需要对此进行修改。
在我的测试中,上面 Jacob 的这个仅适用于数字,不适用于文本,并且不处理引用单元格中的错误(如果任何引用单元格返回错误,则返回错误):
After reading through this and then investigating further, I've got one that works better for me than anything I see here:
Array-enter:
(Ctrl+Shift+Enter, and don't include the curly brackets)
Or in VBA:
It works for both numbers and text, it handles blank cells, it handles errors in referenced cells, and it works in VBA. It's also one of the most compact solutions I've seen. Using it in VBA, it apparently automatically handles the need to be an array formula.
Note, the way it handles errors is by simply including them in the count of uniques. For example, if you have two cells returning #DIV/0! and three cells returning #VALUE!, those 5 cells would add 2 to the final count of unique values. If you want errors completely excluded, it would need to be modified for that.
In my tests, this one from Jacob above only works for numbers, not text, and does not handle errors in referenced cells (returns an error if any of the referenced cells returns an error):
看看 https://excelchamps.com/blog/count-unique-values-excel /。
这就是你的答案。
您需要输入的公式是:
当您以数组形式输入此公式时,它将如下所示:
Look at https://excelchamps.com/blog/count-unique-values-excel/.
There is your answer.
The formula you need to enter is:
When you enter this formula as an array it will look something like this:
自 2018 年秋季以来,使用新的动态数组函数可以更轻松地解决此问题(目前仅适用于 Office 365 客户端 - 该解决方案确实不适用于 Excel 2016 / 2019):
UNIQUE
函数将生成范围内唯一值的数组,可以使用COUNTA
进行计数。如果您的范围内有空白,您可以使用
FILTER
函数将其过滤掉:Since fall 2018, this problem can be solved much easier using the new dynamic array functions (currently only available for Office 365 clients - this solution does not work on Excel 2016 / 2019):
The
UNIQUE
function will result in an array of unique values in the range, which can be counted usingCOUNTA
.If you have blanks in your range, you may filter them out using the
FILTER
function:这个公式对我有用。有一些事情可能会导致此功能不起作用。首先,所有目标单元格中都必须有一个值。另一个可能不起作用的示例是,如果您有一个单元格的值为 31,而另一个单元格的文本值为“31”。它将把这些识别为不同的值。
你可以试试这个:
这是一个数组公式。您必须按 ctrl+shift+enter 键,而不是直接按 Enter 键进行确认。
来自:
http://www.cpearson.com/excel/Duplicates.aspx
The formula works for me. There are a few things that could cause this to not work. First, all target cells must have a value in them. Another example of where this might not work is if you have one cell with the value 31 and another cell with a text value of "31". It will recognize these as different values.
You could try this:
This is an array formula. Instead of hitting just enter to confirm it you must hit ctrl+shift+enter.
Which is from:
http://www.cpearson.com/excel/Duplicates.aspx
这可能是处理大量行的更有效方法。这使用内置的 AdvancedFilter 命令,而不是一次循环遍历每个单元格。
This might be a more efficient way of dealing with a large number of rows. This uses the inbuilt AdvancedFilter command instead of cycling through each cell at a time.
另一种方法是:
您只需让活动单元格位于您正在计数的列的第 1 行即可。
Another way to do this is this:
You just have to have the active cell be on row 1 of the column that you are counting.