在Excel中计算不同值 - 频率函数

发布于 2024-09-11 04:31:30 字数 325 浏览 5 评论 0原文

在Excel中计算不同值 - 频率函数 是的,我读过 在 Excel 中计算不同值 - 频率函数

我尝试计算具有不同数字的列列

包含(搜索)

1 3 7 9 5 1 3 9 4

查找的结果;

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

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

I am try to count a column 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-09-18 04:31:30

您可以使用 COUNTIF 来计算符合条件的元素数量。
假设 A 列中有数字,例如从 A1 到 A10:

A1: 1
A2: 3
A3: 7

等等...
在工作表上的某个位置(例如 B 列)输入您感兴趣的值:

B1: 0
B2: 1

等等...
然后在 C1 中输入
=COUNTIF($A$1:$A$10, B1)

这应该计算 A1:A10 中等于 B1(即 0)的值的数量。

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.

怪我入戏太深 2024-09-18 04:31:30

在 A 列中输入数字,在 B 列中输入序列

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

选择两列并创建一个数据透视表,将 A 列放入行中。选择 {COUNT} 作为函数,您就完成了。

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.

无所谓啦 2024-09-18 04:31:30

不完全是你所问的,但我使用宏来生成频率表。我喜欢它。原始代码由 MWE 发布于 http://www.vbaexpress.com/kb /getarticle.php?kb_id=406 我(希望)对其进行了一些改进。留下了一些冗余代码,这样我就能得到更多回复: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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文