自定义Excel函数数据类型问题

时间:2016-11-01 20:00:20

标签: excel excel-vba excel-formula vba

(function(document) {

    // JQuery code here

})(wrap(document));

我正在尝试创建一个函数来计算动态范围的对数正态分布。我按原样得到#VALUE错误,无法找出原因。谢谢你的帮助。

EDIT ****

我道歉,这是我真正想去的地方。我尝试用你的建议更新,但我仍然遗漏了一些东西。

Function LogNormProb(Data As Range, Probability As Double) As Double

Dim Cell As Range

For Each Cell In Data
    Cell.Value = WorksheetFunction.Ln(Cell.Value)
Next Cell

LogNormProb = WorksheetFunction.Sum(Data)

End Function

2 个答案:

答案 0 :(得分:2)

您无法更改函数中的其他单元格值。您应该将对数正态结果存储在您聚合到的变量中,然后输出最终值。代码看起来像:

Function LogNormProb(Data As Range, Probability As Double) As Double

    Dim Cell As Range
    Dim dbTotal As Double

    For Each Cell In Data
        dbTotal = dbTotal + WorksheetFunction.Ln(Cell.Value)
    Next Cell

    LogNormProb = dbTotal

End Function

答案 1 :(得分:0)

UDF函数只能更改它所驻留的单元格的值 - 您的udf正在尝试更改数据中所有单元格的值。

尝试这样的事情(未经测试)

Function LogNormProb(Data As Range, Probability As Double) As Double

Dim vData as variant
dim j as long
dim k as long

vData=Data.Value2

For j=lbound(vdata) to ubound(vdata)
for k= lbound(vdata,2) to ubound(vdata,2)
    vdata(j,k) = WorksheetFunction.Ln(vdata(j,k))
Next k
next j

LogNormProb = WorksheetFunction.Sum(vData)

End Function