Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a VBA custom function knows that the formula is entered as an array formula?

Tags:

excel

vba

Is it possible to make the following function returns either multiple values or single value according to how the user enters formula?

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
like image 266
Astaroth Avatar asked Nov 27 '25 05:11

Astaroth


1 Answers

Just examine 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
like image 193
Gary's Student Avatar answered Nov 30 '25 00:11

Gary's Student



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!