我目前仍在尝试创建一个函数,该函数使用符合条件的数据来计算百分位数。我的数据更长,但格式如下:
A B
462 2015
675 2015
494 2014
802 2015
380 2014
因此,在这种情况下,百分位数数组是A列,而条件范围是B列。下面的语句是计算A列中对应于B列2015的值的第90个百分位数。让VBA使用.formulaArray属性评估带引号的语句。使用上面的5行示例(我认为您需要5行以上才能计算百分位数):
Cells(1, 10).FormulaArray = "=PERCENTILE.EXC(IF(A2:A5 = 2015,B2:B5),0.9)"
除了我用函数中的范围/值代替变量时,此方法有效。这不起作用:
dim lastyear as integer
dim yearcol as range
dim valuescol as range
lastyear = 2015
valuescol = range(cells(1,1),cells(5,1))
yearcol = range(cells(1,2),cells(5,2)
Cells(1, 10).FormulaArray = "=PERCENTILE.EXC(IF(yearcol = lastyear, valuescol,0.9)"
如果有人有任何建议,我将不胜感激。
谢谢
您需要将变量移到引号之外:
dim lastyear as integer
dim yearcol as range
dim valuescol as range
lastyear = 2015
Set valuescol = range(cells(1,1),cells(5,1))
Set yearcol = range(cells(1,2),cells(5,2))
Cells(1, 10).FormulaArray = "=PERCENTILE.EXC(IF(" & yearcol.address(0,0) & " = " & lastyear & ", " & valuescol.address(0,0) & ",0.9)"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句