Excel:需要一个公式,可以获得范围内的唯一值的总数

时间:2011-03-08 01:07:54

标签: excel formula

示例:A10 = 111,A11 = 101,A12 = 111,A13 = 4,A14 = 101,A15 =空白。总数是3个唯一值。我需要一个可以计算所有唯一值的单元格中的动态公式。空白将在设定范围内,不应包括在总数中。谢谢。

3 个答案:

答案 0 :(得分:2)

来自Microsoft:

计算单元格B2:B10(不得包含空格)的唯一文本和数字值的数量(7)

=SUM(IF(FREQUENCY(MATCH(B2:B10,B2:B10,0),MATCH(B2:B10,B2:B10,0))>0,1))

http://office.microsoft.com/en-us/excel-help/count-occurrences-of-values-or-unique-values-in-a-data-range-HP003056118.aspx

答案 1 :(得分:1)

这可以在VBA中完成:http://www.google.com/search?q=VBA+distinct

如果您只需要这样做一次,您可以通过几个步骤半手动完成:

1)对值进行排序(例如,在B10-B15中)

2)在下一栏中,在每个单元格中使用此公式:=IF(C10<>B9,C10,"")。 (只有在不等于上面的值时才显示该值。)这将为您提供唯一值。

“总金额”是指不同价值的总和或数量?无论哪种方式,您都可以使用计算列的值来执行此操作。

答案 2 :(得分:0)

您可以使用此VBA代码,代码已经过充分评论,供您理解。

Sub sample_CntDist()
'
' Counting Distinct numbers, not counting blank values presenting count distinct in a cell Macro
'
'
Columns("A:A").Select 'select the column which have numbers
Selection.Copy 'copy the selection
Sheets("Sheet2").Select 'select a new sheet
Range("A1").Select 'select first cell
ActiveSheet.Paste 'paste the copied data
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$18").RemoveDuplicates Columns:=1, Header:=xlNo 'remove duplicates
Range("B1").Select 'select cell b1
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-1],""<>"")" 'count distinct number excluding blanks
Sheets("Sheet1").Select 'select original sheet
Range("C2").Select 'select cell c2
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "count distinct " 'give a caption for the result
Range("D2").Select 'select cell d2
Sheets("Sheet2").Select 'select the temp sheet
Selection.Copy 'copy the distinct count
Sheets("Sheet1").Select 'select and paste the result as values
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
ActiveWindow.SelectedSheets.Visible = False 'hide temp sheet
End Sub

相关问题