次の関数は、グラフオブジェクトの系列上の2点を与えられた最小二乗線を計算します。
' Calculates the "a" and "b" of the least squares line
' given a series and two indexes on the series
' returns an array(1 to 2) where a is (1) and b is (2)
Function linest(s As Series, idx1 As Long, idx2 As Long)
ReDim Xs(idx1 To idx2) ' The X values
ReDim Ys(idx1 To idx2) ' The Y values
Dim i As Long
For i = idx1 To idx2
Xs(i) = s.XValues(i)
Ys(i) = s.values(i)
Next
linest = WorksheetFunction.linest(Ys, Xs)
End Function
使用
この試験は、シート1の列からグラフを作成する(Xさん)とB(Y'S)。 次に、上記の関数を使用して、5番目と15番目の間の値の最小二乗直線を計算します。
Sub Test()
' first let us create a chart object and a series
Dim ch As ChartObject, s As Series
Set ch = Sheet1.ChartObjects.Add(10, 10, 500, 250)
Set s = ch.Chart.SeriesCollection.NewSeries
s.XValues = Sheet1.Range("A1:A20")
s.values = Sheet1.Range("B1:B20")
' calculate the least-squares line between 5th and 15th points
Dim eq: eq = linest(s, 5, 15)
' display the results
Debug.Print " The least squares line is:" & vbCrLf & vbCrLf & _
" Y = " & eq(1) & "*X + " & eq(2)
End Sub
あなたは[LINEST関数](https://support.office.com/en-us/article/LINEST-function-84D7D0D9-6E50-4101-977A-FA7ABF772B6D)をお探しですか? –
はい、私は2点間で動作させる方法がわかりません –
これらの2点間の値の「範囲」を取り戻せますか? –