四十路サラリーマン、強く生きる

日々気になっている、やってみたことメモ

線形近似線slope、intercept、R2出力

線形近似曲線の統計値出力に関する記事です。

  • 状況

    VBAを用いてslope, intercept, Rsq関数の計算値を出力したい。

  • 実行
    グラフX,Yの数値範囲をRange1,Range2で定義
    Worksheetfunctionで関数を計算

  • シート画像
    f:id:JUNx2:20201219182823j:plain

  • VBA記述

Sub Test()  
Dim Range1, Range2 As Range  
  
Set Range1 = Range(Cells(4, 2), Cells(10, 2))  
Set Range2 = Range(Cells(4, 3), Cells(10,3))  
  
Slope = WorksheetFunction.Slope(Range2, Range1)  

Intercept = WorksheetFunction.Intercept(Range2, Range1)  

Rsq = WorksheetFunction.Rsq(Range2, Range1)  
  
Cells(4, 6) = Slope  
Cells(7, 6) = Intercept  
Cells(10, 6) = Rsq    
End sub