13

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
4

12 に答える 12

26

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

「=CountUnique(N8:O9)」など、任意の範囲を参照 するワークシート関数として使用できます</p>

テキストと数値を処理し、空白のセルを 1 つの値として扱います

配列関数を扱う必要はありません。

ディクショナリ オブジェクトについては、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
于 2013-07-25T11:55:09.423 に答える
9

=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
于 2009-11-04T19:54:57.407 に答える
8

試す:

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

編集:上記は列の空白のエントリを処理します

于 2009-11-04T19:47:29.840 に答える
6

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

Dictionary次のように、「Microsoft Scripting Runtime」を参照し、用語の前に を付ける必要があり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
于 2016-03-18T11:18:01.047 に答える
2

これを読んでさらに調査した後、ここにあるものよりもうまく機能するものを手に入れました:

配列入力:
(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 で機能します。これは、私が見た中で最もコンパクトなソリューションの 1 つでもあります。VBA で使用すると、配列数式である必要性が自動的に処理されるようです。

エラーを処理する方法は、単にそれらを一意の数に含めることであることに注意してください。たとえば、#DIV/0 を返す 2 つのセルがあるとします。#VALUE! を返す 3 つのセルがある場合、これらの 5 つのセルは、一意の値の最終カウントに 2 を追加します。エラーを完全に除外したい場合は、そのために変更する必要があります。

私のテストでは、上記のジェイコブのこれはテキストではなく数値に対してのみ機能し、参照されたセルのエラーを処理しません (参照されたセルのいずれかがエラーを返す場合はエラーを返します):

=SUM(IF(FREQUENCY(G4:G29,G4:G29)>0,1))
于 2015-03-20T23:11:15.530 に答える
2

フィルタを使用して一意の値を一時的に表示し、フィルタされた値をカウントすることもできます。

于 2009-11-05T19:14:23.580 に答える
1

式は私のために働きます。これが機能しない原因となる可能性のあることがいくつかあります。まず、すべてのターゲットセルに値が含まれている必要があります。これが機能しない可能性がある別の例は、値が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))

これは配列数式です。Enterキーを押して確認する代わりに、Ctrl + Shift+Enterキーを押す必要があります。

からです:

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

于 2009-11-04T19:45:18.510 に答える
1

これは、多数の行を処理するより効率的な方法である可能性があります。これは、一度に各セルを循環する代わりに、組み込みの 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
于 2016-05-30T04:22:03.477 に答える