从用户定义的函数获取返回值时出错

时间:2013-01-30 09:47:02

标签: excel excel-vba excel-2007 vba

我的用户定义函数总是返回'公式中使用的值是错误的数据类型'(我将单元格范围传递给函数)。我搜索了很多线程,我很确定我的代码应该可以工作:

Function SortAndEvaluate(ByRef Probs() As Variant, ByRef ResidualProbs() As Variant, ByRef Costs() As Variant)
    Dim Temp As Double
    Dim i As Integer
    Dim NoExcanges As Integer


    'Exchange values in probs in descending order

    Do
        NoExchanges = True
        ' Loop through each element
        For i = 0 To UBound(Probs) - 1

            'If element is greater than the last exchange, else do nothing

            If Probs(i) > Probs(i + 1) Then
                NoExchanges = False
                ' Exchange probability values
                Temp = Probs(i)
                Probs(i) = Probs(i + 1)
                Probs(i + 1) = Temp
                ' Exchange residual probability values
                Temp = ResidualProbs(i)
                ResidualProbs(i) = ResidualProbs(i + 1)
                ResidualProbs(i + 1) = Temp
                ' Exchange cost values
                Temp = Costs(i)
                Costs(i) = Costs(i + 1)
                Costs(i + 1) = Temp
            End If
        Next i
    Loop While Not (NoExchanges)

    Temp = 0
    For i = 1 To UBound(Probs)
        If i = 1 Then
            Temp = Temp + Probs(i) * Costs(i)
        Else
            Temp = Temp + Probs(i) * ResidualProbs(i - 1) * Costs(i)
        End If
    Next i

    SortAndEvaluate = Temp
End Function

有人可以给我任何反馈吗?

3 个答案:

答案 0 :(得分:0)

您没有使用Option Explicit,并且您的代码中也有Dim NoExcanges As Integer的印刷错误。

当您稍后写NoExchanges = True这隐式Dim时,一个新类型的布尔值与引用NoExchanges - NoExcanges永远不会被使用。

您还可能希望显式地删除函数的返回类型:

Function SortAndEvaluate(ByRef Probs() As Variant, ByRef ResidualProbs() As Variant, ByRef Costs() As Variant) As Double

答案 1 :(得分:0)

原因是Excel VBA不允许用户定义的函数更改另一个单元格的值。 UDF被允许做的唯一事情(除了一些小的例外)是将值返回给它所调用的单元格。

函数内部不允许使用行Probs(i) = Probs(i + 1)

查看类似的question

您可以使用Sub执行此操作,但您必须传递单元格以另一种方式排序(可能使用Selection)。

答案 2 :(得分:0)

尝试类似

的内容
Option Explicit

Function SortAndEvaluate(ByRef Probs As Range, ByRef ResidualProbs As Range, ByRef Costs As Range)

    Dim NumberOfProbs As Long
    Dim NumberOfProbsRows As Long
    Dim NumberOfProbsColumns As Long
    Dim RangeAddressProbs As String

    ' Dim for ResidualProbs and Costs
    ' ...

    NumberOfProbs = Probs.Cells.Count
    NumberOfProbsRows = Probs.Rows.Count
    NumberOfProbsColumns = Probs.Columns.Count
    RangeAddressProbs = Probs.Address

    ' also for ResidualProbs and Costs
    ' ...

    ' Might want some validation, e.g.
    If NumberOfProbsColumns <> 1 Then Exit Function
    If NumberOfResidualProbsColumns <> 1 Then Exit Function

    ' Get Values from range as an array
    Dim ValuesProbs As Variant
    Dim ValuesResidualProbs As Variant
    Dim ValuesCosts As Variant

    ValuesProbs = Probs
    ValuesResidualProbs = ResidualProbs
    ValuesCosts = Costs

    ' Do your logic stuff using the Values... arrays
    ' ...

    ' This example return value shows how to reference the Values... arrays (note you need to index the column too)
    SortAndEvaluate = ValuesProbs(NumberOfProbsRows, 1)

End Function