问题
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