开发者

Counting distinct values in excel - frequency function

开发者 https://www.devze.com 2023-01-08 16:41 出处:网络
Counting distinct values in excel - frequency function yes I have read Counting distinct values in excel - frequency function

Counting distinct values in excel - frequency function yes I have read Counting distinct values in excel - frequency function

I am try to count a c开发者_Python百科olumn with different numbers

column contains (search)

1 3 7 9 5 1 3 9 4

result looking for;

C1  C2
1 = 2
2 = 0
3 = 2
4 = 1 
etc


You can use COUNTIF to count the number of elements that match a condition.
Suppose you have your numbers in column A, say from A1 to A10:

A1: 1
A2: 3
A3: 7

etc...
Type in somewhere on your sheet, say in column B, the values you are interested in:

B1: 0
B2: 1

etc...
and in C1, type in
=COUNTIF($A$1:$A$10, B1)

This should count the number of values equal to B1 (i.e. 0), in A1:A10.


Enter your numbers in column A and a sequence in column B

A   B
1   1
2   1
3   1
4   1 
2   1
3   1
4   1

Select both columns and create a pivot table putting col A in rows. Select {COUNT} as function and you are done.


Not exactly what you are asking but i use a macro to generate frequency tables. I like it. Original code was posted by MWE at http://www.vbaexpress.com/kb/getarticle.php?kb_id=406 and i have (hopefully) improved it a bit. Have left in a little bit of redundant code so i get more replies :p

Sub zzzFrequencyDONT_SELECT_WHOLE_COLUMN()

    ' if user selects massive range - usually whole column - stops them


    If Selection.Rows.Count > 60000 Then
    MsgBox "Range selected is way too large - over 60,000. You have probably selected an entire column. Select a range of under 60,000 cells and try again"
    End If

    If Selection.Rows.Count > 60000 Then
    Exit Sub
    End If

     '
     '       Function    computes frequency count of unique values in a selection
     '
    Dim Count() As Integer
    Dim I As Integer, J As Integer
    Dim Num As Integer, NumOK As Integer, MaxNumOK As Integer, NumBad As Integer
    Dim Row As Integer, Col As Integer, Temp1 As Integer, Temp2 As Integer
    Dim strBuffer As String, strBadVals As String
    Dim CellVal As Variant
    Dim Ans As VbMsgBoxResult

    Num = 0
    NumBad = 0
    NumOK = 0
    MaxNumOK = 50
    ReDim Count(MaxNumOK, 2)
    strBuffer = ""
     '
     '           sequence through each cell in selection
     '
    For Each Cell In Selection
        Num = Num + 1
        On Error Resume Next
        CellVal = Cell.Value
        Select Case Err
        Case Is = 0
             '
             '                   no error, examine type
             '
            Select Case LCase(TypeName(CellVal))
            Case "integer", "long", "single", "double"
                 '
                 '                           numeric type; if single or double, use
                 '                           Fix function to reduce to integer portion
                 '
                If TypeName(CellVal) = "single" Or _
                TypeName(CellVal) = "double" Then
                    CellVal = Fix(CellVal)
                End If
                 '
                 '                           check if previously seen
                 '                           if so, simply bump counter
                 '                           if not, increment NumOK and store value
                 '
                For I = 1 To NumOK
                    If CellVal = Count(I, 1) Then
                        Count(I, 2) = Count(I, 2) + 1
                        GoTo NextCell
                    End If
                Next I
                NumOK = NumOK + 1
                If NumOK > MaxNumOK Then
                    MsgBox "capacity of freq count proc exceeded" & vbCrLf & _
                    "Displaying results so far", vbCritical
                    GoTo SortCount
                End If
                Count(NumOK, 1) = CellVal
                Count(NumOK, 2) = 1
            Case Else
                NumBad = NumBad + 1
                If Cell.Text <> "" Then
                    strBadVals = strBadVals & Cell.Text & vbCrLf
                Else
                    strBadVals = strBadVals & "<blank>" & vbCrLf
                End If
            End Select
        Case Is <> 0
            NumBad = NumBad + 1
            If Cell.Text <> "" Then
                strBadVals = strBadVals & Cell.Text & vbCrLf
            Else
                strBadVals = strBadVals & "<blank>" & vbCrLf
            End If
        End Select
NextCell:
    Next Cell
     '
     '           counting done, sort data
     '
SortCount:
    For I = 1 To NumOK
        For J = I To NumOK
            If I <> J Then
                If Count(I, 1) > Count(J, 1) Then
                    Call SwapVals(Count(I, 1), Count(J, 1))
                    Call SwapVals(Count(I, 2), Count(J, 2))
                End If
            End If
        Next J
    Next I
     '
     '           store count data for display
     '

Dim percentstore As Single

percentstore = Str(Count(I, 2)) / Str(Num)

    For I = 1 To NumOK
        strBuffer = strBuffer & Str(Count(I, 1)) & vbTab + Str(Count(I, 2)) & vbTab & FormatPercent(Str(Count(I, 2)) / Str(Num)) & vbCr
    Next I
     '
     '           display results
     '
    MsgBox "CTRL C to copy" & vbCrLf & _
    "# cells examined = " & Str(Num) & vbCrLf & _
    "# cells w/o acceptable numerical value = " & NumBad & vbCrLf & _
    "# unique values found = " & NumOK & vbCrLf & _
    "Frequency Count:" & vbCrLf & "value" & vbTab & "frequency" & vbTab & "Percent" & vbCr + strBuffer, vbInformation, "Frequency count - CTRL C to copy"
    If NumBad > 0 Then
        Ans = MsgBox("display non-numerics encountered?", vbQuestion & vbYesNo)
        If Ans = vbYes Then MsgBox "Non Numerics encountered" & vbCrLf & strBadVals
    End If
     '
     '           write to worksheet?
     '
  '  Ans = MsgBox("Ok to write out results below selection?" & vbCrLf + _
  '  "results will be two cols by " & (NumOK + 1) & " rows", vbQuestion + vbYesNo)
  '  If Ans <> vbYes Then Exit Sub
  '  Row = Selection.Row + Selection.Rows.Count
  '  Col = Selection.Column
'   Cells(Row, Col) = "Value"
  '  Cells(Row, Col + 1) = "Count"
'   For I = 1 To NumOK
  '      Cells(Row + I, Col) = Count(I, 1)
'       Cells(Row + I, Col + 1) = Count(I, 2)
'   Next I

End Sub

Sub SwapVals(X, Y)
     '
     '       Function    swaps two values
     '
    Dim Temp

    Temp = X
    X = Y
    Y = Temp

End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号