简体   繁体   English

如何计算 Excel 中的不同值

[英]How to count distinct values in Excel

Input输入

在此处输入图像描述

I need Summary like below我需要如下摘要

在此处输入图像描述

I am looking for distinct no of account here instead of duplicate.我在这里寻找不同的帐户而不是重复的。

You can FILTER the original data and then count the number of unique instances.您可以FILTER原始数据,然后计算唯一实例的数量。

Try =COUNT(UNIQUE(FILTER($A$2:$A$10, $B$2:$B$10=$D2)))尝试=COUNT(UNIQUE(FILTER($A$2:$A$10, $B$2:$B$10=$D2)))

Here I assume that the original data is in cells A2:B10, and that the criteria for the filtering is in column D.这里我假设原始数据在单元格 A2:B10 中,过滤条件在 D 列中。

在此处输入图片说明

I have updated my answer to work for Office 2007 and mixed @Rory's comment to original post with my OFFSET part of my previous Office 365 solution.我已经更新了适用于Office 2007 的答案,并将 @Rory 对原始帖子的评论与我以前的 Office 365 解决方案的OFFSET部分混合在一起。

Formula公式

Plase the following formula in E2 :E2输入以下公式:

=SUM(1/COUNTIFS(OFFSET($A$3,MATCH(D3,$A$3:$A$12,0)-1,1,COUNTIF($A$3:$A$12,D3),1),OFFSET($A$3,MATCH(D3,$A$3:$A$12,0)-1,1,COUNTIF($A$3:$A$12,D3),1)))

Explanation解释

  • The OFFSET part provides a list of consecutive rows that feature the Acc No and belong to the same Br code . OFFSET部分提供了一个连续行的列表,这些行具有Acc No并且属于相同的Br 代码 It uses MATCH to determine the first occurance of the given Br code .它使用MATCH来确定给定Br 代码的第一次出现。 As the anchor cell for the formula is the cell with the first content ( $A$3 ), I subtract 1 from the MATCH result.由于公式的单元格是包含第一个内容 ( $A$3 ) 的单元格,因此我从MATCH结果中减去 1。 To determine the height, I use a COUNTIF statement, that counts how many rows feature the current Br code .为了确定高度,我使用了COUNTIF语句,该语句计算当前Br 代码具有多少行。

  • The cell range provided by OFFSET is then used as input for @Rory's COUNTIFS solution. OFFSET提供的单元格范围然后用作COUNTIFSCOUNTIFS解决方案的输入。

The text and non-text (numeric) unique and distinct values in the column are quickly listed separately using VBA macro.使用 VBA 宏快速分别列出列中的文本和非文本(数字)唯一且不同的值。

enter image description here在此处输入图像描述

Sub GetCountDistinctValues()
 Dim sayi As Long, rang As Range
        With CreateObject("Scripting.Dictionary")
        For Each rang In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
            If rang <> Empty Then
                If Not .Exists(rang.Value) Then
                    .Add rang.Value, Nothing
                    If IsNumeric(rang.Value) Then sayi = sayi + 1
                End If
            End If
        Next
        Range("C2").Value = .Count - sayi
        Range("C3").Value = sayi
    End With
    End Sub

Source: Find count of unique-distinct values资料来源: 查找唯一不同值的计数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM