具有多个输出的Excel函数,没有数组公式

时间:2013-10-11 16:31:43

标签: arrays excel vba

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

4 个答案:

答案 0 :(得分: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

但是这与拥有许多不同的功能并没有太大区别,而且对于任何复杂的功能来说,使用数组公式的效率要低得多。

答案 1 :(得分: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

您可以使用FIND和MID功能检索各个部分。

答案 2 :(得分:0)

简答:不。

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

答案 3 :(得分:0)