1

I'm new to Excel VBA and its been 5 years since I've done any VBA at all. I've written a UDF to do a basic regression, but I can't get it to output an array of regressed values. I select the range I want to output so and hit crtl+shift+enter, but it doesn't work. I've tried a few different things, but nothing does the trick. Here is my latest attempt:

Function REGRESSMEDIAN(x As Range, y As Range) As Double

Dim slope As Double, intercept As Double, count As Integer

count = x.count

Dim lny() As Double, regression() As Double
ReDim lny(1 To count), regression(1 To count)

Dim i As Integer

For i = 1 To count
    lny(i) = Application.WorksheetFunction.Ln(y(i))
Next i


slope = Application.WorksheetFunction.slope(lny, x)
intercept = Application.WorksheetFunction.intercept(lny, x)

Dim j As Integer

For j = 1 To count
    regression(j) = Exp(slope * x(j) + intercept)
Next j

    REGRESSMEDIAN = regression

End Function
3
  • 1
    What doesn't work? The code doesn't run? Commented Apr 27, 2017 at 22:48
  • This version doesn't run, but I had a version that did. My issue is that it won't output the array of regressed values. Even when I had it outputting an array, every cell was just the first value in the array. Commented Apr 27, 2017 at 22:51
  • Try changing the return type of the Function to Variant. So Function REGRESSMEDIAN(x As Range, y As Range) As Variant Commented Apr 27, 2017 at 22:59

2 Answers 2

2

This test function:

Function tester()
    tester = Array("a", "b", "c")
End Function

will work fine as a UDF as long as your 3 output cells are in a row, not in a column. If they're in a column then you'll just see "a" in all 3 cells.

enter image description here

If you're trying to put the output in a column then this will work:

Function tester()
    tester = Application.Transpose(Array("a", "b", "c"))
End Function

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I changed the function type to variant and that gave me the first value in all columns. The transpose fixed that, it seems like the obvious answer in hindsight, I just wasn't aware that Excel VBA stores arrays as a row by default.
I would not say it's "obvious" - just have to remember that Excel has a preference for putting things in rows...
0

Try

Function REGRESSMEDIAN(x As Range, y As Range) As Double()

only putting the parentheses in the function declaration.

It will work.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.