简体   繁体   中英

Excel WorksheetFunction.Trend function in VBA

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.

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.

VBE-查看-当地人

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM