VB中的趋势线常量

时间:2014-03-14 00:13:17

标签: vb.net excel

我是VB新手但看起来应该很简单。我正在尝试在VB项目中获得线性趋势线的常量。

    Dim WStemp1 As WorksheetFunction
    Dim WStemp2 As WorksheetFunction

    X1_home = WStemp1.Index(WStemp2.LinEst(y_values, x_values), 1, 1)
    C1_home = WStemp1.Index(WStemp2.LinEst(y_values, x_values), 1, 2)

此代码在vba中运行正常,但我收到以下错误

    An unhandled exception of type 'System.NullReferenceException' occurred in myApp.exe
    Additional information: Object reference not set to an instance of an object.
    If there is a handler for this exception, the program may be safely continued.

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您必须从Excel应用程序中设置WorksheetFunction对象。例如:

Dim App As New Excel.Application
Dim ExcelFunc As Excel.WorksheetFunction = App.WorksheetFunction

'Use the excel function
Dim avg As Double 
avg = ExcelFunc.Average(1, 2, 3, 4, 5)

'release objects and terminate Excel application
ExcelFunc = Nothing
App.Quit()
App = Nothing

干杯