VBA自定义函数是否可以知道公式是作为数组公式输入的?

时间:2015-06-13 13:19:11

标签: excel vba excel-vba

是否可以根据用户输入公式的方式使以下函数返回多个值或单个值?

Public Function Test(ByVal flNumber As Double) As Variant
  Dim flResult1 As Double
  Dim sResult2 As String
  Dim bArrayFormula As Boolean

  flResult1 = Round(flNumber / 10 ^ 6, 1)
  sResult2 = "million"

  ' How to know that the formula is entered as an array formula?
  If bArrayFormula Then
    Test = Array(flResult1, sResult2)
  Else
    Test = flResult1 & " " & sResult2
  End If
End Function

1 个答案:

答案 0 :(得分:4)

只需检查 Application.Caller

即可
Public Function SmartFunction() As String
    addy = Application.Caller.Address
    If Range(addy).HasArray Then
        SmartFunction = "Array Formula"
    Else
        SmartFunction = "Normal Formula"
    End If
End Function