1

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

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

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

4

3 回答 3

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

于 2013-01-30T10:02:01.807 回答
0

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

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

查看类似问题

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

于 2013-01-30T11:14:13.840 回答
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
于 2013-01-30T12:07:26.947 回答