Excel WorksheetFunction.Trend function in VBA

假如想象 提交于 2019-12-25 07:07:08

问题


I have the formula =TREND({4000,20000},{0,32},B15) in an Excel cell.

What is the correct way to use this in a VBA function?

I keep getting Runtime Error 1004: Unable to get the Trend property of the WorksheetFunction class, whenever Itry variations along the lines of:

WorksheetFunction.Trend("{4000,20000}", "{0,32}", "B15")

Thanks.


回答1:


I couldn't get your formula to work in an Excel cell (#REF error). Don't the known x's have to have the same number of values as the known y's? This worked

?application.WorksheetFunction.Trend(array(4000,20000),array(0.30,0.32),Range("B15").Value)(1)

I'm using the array function to create arrays for the first two arguments. I made up another value for the second argument. The whole thing then returns an array, so the (1) at the end is to return the first element of the array.

You can inspect the result with a sub like this

Sub TestTrend()

    Dim aY(1 To 2) As Double
    Dim aX(1 To 2) As Double
    Dim vResult As Variant

    Range("B15").Value = 0.35

    aY(1) = 4000: aY(2) = 20000
    aX(1) = 0.3: aX(2) = 0.32

    vResult = Application.WorksheetFunction.Trend(aY, aX, Range("B15").Value)

    Stop

End Sub

When you View - Locals, you can see that the result holds a one-element variant array.



来源:https://stackoverflow.com/questions/26866113/excel-worksheetfunction-trend-function-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!