0

是否可以定义具有多个输出的 Excel VBA 函数并在没有数组公式的情况下使用它的工作表范围?

4

4 回答 4

1

不是真正的“多个输出”,但您可以使用 Application.Caller 来确定从哪里调用您的函数,并根据(例如)公式所在的列返回单个值。

Function WhereAmI(rng As Range)
    Dim c, ws, rv
    Set c = Application.Caller 'cell containing the formula
    Set ws = c.Parent          'worksheet containing the formula

    Select Case c.Column
        Case 2: rv = "OK"
        Case Else: rv = "not OK"
    End Select

    WhereAmI = rv
End Function

但这与拥有许多不同的函数并没有太大区别,对于任何复杂的东西,它的效率都会比使用数组公式低得多。

于 2013-10-11T16:46:01.057 回答
1

您可以将多个值放入一个字符串中:

Public Function CircleStuff(r As Range) As String
    Dim diameter As Double, circumference As Double, area As Double
    Dim radius As Double
    radius = r.Value
    diameter = 2 * radius
    circumference = 3.1415926 * diameter
    area = 3.1415926 * radius * radius
    CircleStuff = CStr(diameter) & "," & CStr(circumference) & "," & CStr(area)
End Function

所以如果 A1 包含:

1

=CircleStuff(A1) 将返回:

2,6.2831852,3.1415926

you could retrieve the individual pieces with FIND and MID functions.

于 2013-10-11T17:18:09.137 回答
0

简短的回答:没有。

长答案:嗯,你可以做一些类似的事情A1=CalculateOutput1() & "," & CalculateOutput2()B1=SomeFunction(Left(A1, Find(A1, ",") - 1), Right(A1, Len(A1) - Find(A1, ",")))但是......我不明白你为什么要这样做。

于 2013-10-11T16:39:25.653 回答
0

Using xlwings it is possible to define dynamic arrays:

http://docs.xlwings.org/en/stable/udfs.html#dynamic-array-formulas

于 2017-04-10T15:24:39.730 回答