[英]Count unique values in 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
這是一個對我有用的 VBA 函數。
您可以將其用作工作表函數,引用任何范圍,例如“=CountUnique(N8:O9)”
它處理文本和數字值,並將空白單元格視為一個值
它不需要處理數組函數。
對於字典對象,它需要對 Microsoft 腳本庫的引用。
Public Function CountUnique(rng As Range) As Integer
Dim dict As Dictionary
Dim cell As Range
Set dict = New Dictionary
For Each cell In rng.Cells
If Not dict.Exists(cell.Value) Then
dict.Add cell.Value, 0
End If
Next
CountUnique = dict.Count
End Function
=SUM(IF(FREQUENCY(IF(LEN(A2:A10)>0,MATCH(A2:A10,A2:A10,0),""), IF(LEN(A2:A10)>0,MATCH(A2:A10,A2:A10,0),""))>0,1))
http://office.microsoft.com/en-us/excel/HP030561181033.aspx
你也可以寫一個 VBA 宏(但不確定這是否是你想要的。)
效果如下(給定一個 A1-A11 填充且 B1-B11 為空的電子表格):
Sub CountUnique()
Dim count As Integer
Dim i, c, j As Integer
c = 0
count = 0
For i = 1 To 11
Sheet1.Cells(i, 2).Value = Sheet1.Cells(i, 1).Value
c = c + 1
For j = 1 To c
If CDbl(Sheet1.Cells(i, 1).Value) = CDbl(Sheet1.Cells(j, 2).Value) Then
c = c - 1
Exit For
End If
Next j
Next i
' c now equals the unique item count put in the 12'th row
Sheet1.Cells(12, 1).Value = c
End Sub
嘗試:
=SUM(IF(FREQUENCY(C2:C2080,C2:C2080)>0,1))
編輯:以上將處理列中的空白條目
由於 Excel 中的某種類型的限制,JustinG 的函數運行得非常好(而且很快),直到唯一項目的數量超過 32,767。
我發現如果你修改他的代碼
Public Function CountUnique(rng As Range) As Integer
並使其成為...
Public Function CountUnique(rng As Range) As Long
然后它將處理更多獨特的項目。
對於仍在嘗試使用@JustinG 的字典方法的任何人,如果您使用的是較新版本的 VBA,則需要稍微更改代碼。
您需要引用“Microsoft Scripting Runtime”並在Dictionary
術語前加上Scripting
前綴,如下所示:
Public Function CountUnique(rng As Range) As Long
Dim dict As Scripting.Dictionary
Dim cell As Range
Set dict = New Scripting.Dictionary
For Each cell In rng.Cells
If Not dict.Exists(cell.Value) Then
dict.Add cell.Value, 0
End If
Next
CountUnique = dict.Count
End Function
您也可以簡單地使用過濾器來臨時顯示唯一值,並對過濾后的值進行計數。
通讀本文並進一步調查后,我得到了一個比我在這里看到的任何東西都更適合我的方法:
數組輸入:
(Ctrl+Shift+Enter,不包括大括號)
{=SUM(IFERROR(1/COUNTIF(C2:C2080,C2:C2080),0))}
或者在 VBA 中:
MyResult = MyWorksheetObj.Evaluate("=SUM(IFERROR(1/COUNTIF(C2:C2080,C2:C2080),0))")
它適用於數字和文本,處理空白單元格,處理引用單元格中的錯誤,並且適用於 VBA。 這也是我見過的最緊湊的解決方案之一。 在 VBA 中使用它,它顯然會自動處理成為數組公式的需要。
請注意,它處理錯誤的方式是簡單地將它們包含在唯一值計數中。 例如,如果您有兩個單元格返回 #DIV/0! 和三個單元格返回 #VALUE!,這 5 個單元格會將 2 添加到唯一值的最終計數。 如果您希望完全排除錯誤,則需要為此進行修改。
在我的測試中,上面來自 Jacob 的這個僅適用於數字,不適用於文本,並且不處理引用單元格中的錯誤(如果任何引用單元格返回錯誤,則返回錯誤):
=SUM(IF(FREQUENCY(G4:G29,G4:G29)>0,1))
查看https://excelchamps.com/blog/count-unique-values-excel/ 。 這就是你的答案。
您需要輸入的公式是:
=SUMPRODUCT(1/COUNTIF(C2:C2080,C2:C2080))
當您將此公式作為數組輸入時,它將如下所示:
{=SUMPRODUCT(1/COUNTIF(C2:C2080,C2:C2080))}
這個公式對我有用。 有一些事情可能會導致這不起作用。 首先,所有目標單元格中都必須有一個值。 這可能不起作用的另一個示例是,如果您有一個值為 31 的單元格和另一個文本值為“31”的單元格。 它會將這些識別為不同的值。
你可以試試這個:
=SUM(IF(FREQUENCY(IF(LEN(B2:B11)>0,MATCH(B2:B11,B2:B11,0),""), IF(LEN(B2:B11)>0,MATCH(B2:B11,B2:B11,0),""))>0,1))
這是一個數組公式。 您必須按 ctrl+shift+enter 鍵,而不是按 Enter 鍵進行確認。
來自:
這可能是處理大量行的更有效方法。 這使用內置的 AdvancedFilter 命令,而不是一次循環遍歷每個單元格。
Public Function UniqueValues(oRange As Range) As Variant
' Uses the built-in AdvancedFilter Excel command to return the unique values onto the Worksheet
' and then populate and retuns an array of unique values
' Note: The index:0 element in the returned array will be the header row.
' You can ignore this element unless the first row in your oRange is a unique value
' in which case the header will be that value.
Dim oTarget As Range
Dim r As Long, numrows As Long
Dim vs1, vs2 As Variant
' Get the first unused cell on the first row where the unique vaues will be temporarily populated
Set oTarget = oRange.SpecialCells(xlLastCell) ' the last cell on the worksheet
Set oTarget = oTarget.Parent.Cells(1, oTarget.Column + 1) ' the first unused cell on the first row
' Copy the unique values from your oRange to the first unused cell on row 1
oRange.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=oTarget, Unique:=True
' Get the number of rows including the first row which is the header
numrows = WorksheetFunction.CountA(oTarget.EntireColumn)
' create an 2-dim array of the rows
vs1 = oTarget.Resize(numrows)
' Prepare a second 1-dim array for the result
ReDim vs2(numrows)
' Transfer the 2-dim array into the 1-dim array
For r = 1 To UBound(vs1, 1)
vs2(r - 1) = vs1(r, 1)
Next
' Return the 1-dim array as the function result
UniqueValues = vs2
' Clean up the extra column on the worksheet
oTarget.EntireColumn.Delete
End Function
另一種方法是這樣的:
Sub CountUnique()
Dim Count, x, a, lastRow, Values(), StringValues
a = ActiveCell.Column
a = GetLetterFromNumber(a)
lastRow = Range(a & Rows.Count).End(xlUp).row
Count = 0
For Each c In Range(Range(a & "1"), Range(a & Rows.Count).End(xlUp))
If c.row = 1 Then
ReDim Values(lastRow)
Values(Count) = c.Value
Count = Count + 1
End If
StringValues = Join(Values, "#")
StringValues = "#" + StringValues
If InStr(1, StringValues, c.Value) = 0 Then
Values(Count) = c.Value
Count = Count + 1
End If
Next c
MsgBox "There are " & Count & " unique values in column " & a
End Sub
您只需要讓活動單元格位於您正在計數的列的第 1 行。
自 2018 年秋季以來,使用新的動態數組函數可以更輕松地解決此問題(目前僅適用於 Office 365 客戶端- 此解決方案不適用於 Excel 2016 / 2019):
=COUNTA(UNIQUE(C2:C2080))
UNIQUE
函數將生成范圍內唯一值的數組,可以使用COUNTA
對其進行計數。
如果您的范圍中有空白,您可以使用FILTER
函數過濾掉它們:
=COUNTA(UNIQUE(FILTER(C2:C2080,C2:C2080<>"")))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.