2009-11-04 3 views
8

Excelの範囲(C2:C2080)のユニークな値を数える必要があります。グーグル公式:Excelで一意の値を数えよう

=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 

答えて

6

=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 
+0

でないと私に0が与えられますが、私はあなたの中から良いものを作ることができました。高すぎる。 – dayan

0

式が私のために動作します。これが動作しない原因になることがいくつかあります。まず、すべてのターゲットセルに値が必要です。これがうまくいかない場合の別の例は、値が31のセルが1つあり、テキスト値が「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を押す必要があります。

http://www.cpearson.com/excel/Duplicates.aspx

5

試してみてください:

=SUM(IF(FREQUENCY(C2:C2080,C2:C2080)>0,1))

EDIT:上記列に空白のエントリを処理します

+0

これを試してみると、答えが – Mike

13

私のために働くVBA関数です。

あなたが任意の範囲を参照し、worksheet functionとしてそれを使用することができ、例えば「= COUNTUNIQUE(N8:O9)」

それはそれがないテキストや数値を処理し、一つの値

として空白セルを扱います配列関数を扱う必要はありません。

dictionary objectについては、Microsoft Scripting Libraryへの参照が必要です。

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 
4

JustinGの機能は、Excelで何らかのタイプの制限があるため、ユニークなアイテムの数が32,767を超えるまで機能します。

あなたは彼のコードは

Public Function CountUnique(rng As Range) As Integer 

を変更し、それは、より多くのユニークなアイテムを処理する

Public Function CountUnique(rng As Range) As Long 

...としてそれを作る場合、私が見つかりました。

0

本を読んし、さらに調査した後、私はここを参照してください何よりも私のために良い作品1を持っている:

アレイ-入力してください:
(Ctrlキー+ + Shiftキーを入力し、しないでください中括弧)

{=SUM(IFERROR(1/COUNTIF(C2:C2080,C2:C2080),0))} 

あるいはVBAで含まれます:それは数字とテキストの両方のために働く

MyResult = MyWorksheetObj.Evaluate("=SUM(IFERROR(1/COUNTIF(C2:C2080,C2:C2080),0))") 

を、それが空白のセルを処理し、参照されるセルのエラーを処理し、VBAで動作します。それは私が見た最もコンパクトなソリューションの1つです。これをVBAで使用すると、配列式の必要性が自動的に処理されます。

エラーを処理する方法は、ユニーク数に単純にエラーを含めることです。たとえば、#DIV/0を返す2つのセルがあるとします。 #VALUE!を返す3つのセルは、これらの5つのセルが一意の値の最終カウントに2を加算します。エラーを完全に除外したい場合は、それを修正する必要があります。私のテストで

、上記のみヤコブからのこの1つは数字ではなく、テキストのために働く、と参照細胞中のエラーを処理しません(参照セルのいずれかがエラーを返した場合、エラーが返されます):

=SUM(IF(FREQUENCY(G4:G29,G4:G29)>0,1)) 
2

まだ@ JustinGの辞書メソッドを使用しようとしている人は、新しいバージョンのVBAを使用している場合は、コードを少し変更する必要があります。

次のようにあなたは、「Microsoftスクリプトランタイム」を参照してScriptingDictionary用語の前に付ける必要があります:

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 
+1

「Microsoft Scripting Runtime」を参照するには、VBAProjectにアクセスし、[ツール] - > [参照]をクリックします。 - > 'Microsoft Scripting Runtime' – Axel

0

これは、多数の行を扱うのより効率的な方法かもしれません。これは、一度に各セルを循環する代わりに、inbuilt 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 
0

これを行うための別の方法はこれです:

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行目になる持っている必要があります。

関連する問題