使用两列计算中位数

我有以下专栏

12 A 11 M 12 B 12 C 11 A 9 M 13 N 11 M 12 C 11 B 15 M 

我想根据我select范围的女士来计算中位数。

我想要使​​用列而不是范围。 有没有解决办法? 谢谢你的帮助

如果你有2010年或以后使用这个公式:

 =AGGREGATE(17,6,(A:A/(B:B="M")),2) 

如果你有2007或更早的版本,试试这个数组公式:

 =MEDIAN(IF(B:B="M",A:A)) 

这是一个数组公式,并且必须用Ctrl-Shift-Enter确认。

尽pipe使用全列引用会减慢计算时间,因为它将遍历所有行,全部104万。 将参考限制在最大数据范围以帮助抵消这种stream失是一种好的做法。 将C:C更改为像$ C $ 1:$ C $ 10000之类的东西,那么它将只循环10,000次。

那么作为一名VBA用户,我喜欢使用VBA,其他人可以使用你的技能使用公式,如果你擅长公式,那么就完全正确。 这是我的答案,我希望能帮到你。

想象一下,你有这样的:

 +----+------------+--------------------+---------------+ | 1 | Values (A) | Criteria Range (B) | Criteria ( C) | +----+------------+--------------------+---------------+ | 2 | 14 | B | A | | 3 | 1 | M | | | 4 | 15 | A | | | 5 | 15 | E | | | 6 | 10 | A | | | 7 | 3 | M | | | 8 | 11 | A | | | 9 | 8 | M | | | 10 | 14 | A | | | 11 | 5 | M | | | 12 | 9 | M | | | 13 | 10 | M | | | 14 | 11 | N | | | 15 | 9 | A | | | 16 | 2 | M | | | 17 | 15 | M | | | 18 | 11 | A | | | 19 | 12 | S | | | 20 | 9 | M | | | 21 | 11 | A | | | 22 | 15 | V | | +----+------------+--------------------+---------------+ 

在单元格E2中,您有=MedianIf($A$2:$A$22,$B$2:$B$22,C2) ,结果必须是11

因为在VBA的一个常规模块中你把这个function。

 Function MEDIANIF(RangeIf As Range, Criteria_Range1 As Range, Criteria1 As String) As Double Dim i Dim Counter Dim tmp() Dim subCouter subCouter = 0 For Each i In Criteria_Range1 Counter = Counter + 1 If i.Value = Criteria1 Then subCouter = subCouter + 1 ReDim Preserve tmp(1 To subCouter) tmp(subCouter) = RangeIf(Counter) End If Next i Dim a MEDIANIF = Application.WorksheetFunction.Median(tmp()) End Function 

而function是这样的:

 RangeIF = is the range with the values that you want to get the MEDIAN Criteria_Range1 = Is the range where you have all the criteria, is the same size as RangeIF Criteria1 = Is the criteria you use to filter the data. 

这个低科技解决scheme如何?

  1. 在列C中,公式: = IF(B2 =“M”,A2,“”) (在第2-12行中,根据您的数据,假设第1行包含标题)
  2. 在单元格D2(或其他任何地方),制定: = MEDIAN(C:C)