Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Range works in formula but not in custom function

I have a range in a formula and it works:

=MAX(LN(A2:A7))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now I want to rebuild this formula as a custom function and it does not work.

Function testy(xr As Range)
    n = WorksheetFunction.Max(WorksheetFunction.Ln(xr))
    testy = n   
End Function

It throws an error: wrong datatype. But I defined "As Range"?
Why does it not work, how can I correct it?

>Solution :

Use Application.Ln.

The issue is that WorksheetFunction.Ln accepts a Double as its argument. You’re implicitly passing it the .Value of a multi-cell range, which is a Variant array.

Using the late-bound Application.Ln circumenvents this behavior.

Function testy(ByVal xr As Range) As Double
    testy = WorksheetFunction.Max(Application.Ln(xr))
End Function

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading