自分の忘備録です。
H-Vというシートにダムの既知のH-V曲線があるとします。
Sheet1に自動観測装置かなんかで計測した貯水位データ(毎時・日平均など)があるとします。
- この計測水位データを、H-V曲線のプロットと比較
- ある2点のプロットの間に収まるならば、2点間の直線の式と計測水位データから貯水量を計算
Sub 貯水量()
Dim h() As Variant
Dim v() As Variant
Dim i_hvarry As Long
Dim i_row As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual '手動計算にする
'H-Vデータを配列に格納。この配列を使用するときは次元も指定。
Sheets("H-V").Select
h = Range2Array(5, 7)
v = Range2Array(5, 8)
'水位データH_measuredから、HVのプロットを用いて貯水量を算出(線形補間)
Sheets("Sheet1").Select
For i_row = 2 To Cells(2, 2).End(xlDown).Row
H_measured = Cells(i_row, 2)
For i_hvarry = 1 To UBound(h, 1) - 1 '-1しないと繰り返し範囲がエラー
If h(i_hvarry, 1) <= H_measured And H_measured < h(i_hvarry + 1, 1) Then
V1 = v(i_hvarry, 1)
H1 = h(i_hvarry, 1)
V2 = v(i_hvarry + 1, 1)
H2 = h(i_hvarry + 1, 1)
v_cal = ValueBetween2(V1, H1, V2, H2, H_measured) '自作関数
End If
Next i_hvarry
Cells(i_row, 5) = v_cal
v_cal = Empty
Next i_row
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic '自動計算に戻す
End Sub
'レンジを配列に変換する。
'Range2Array(始行、始列)
Function Range2Array(start_row, start_column) As Variant
Range2Array = Range(Cells(start_row, start_column), Cells(Cells(start_row, start_column).End(xlDown).Row, start_column))
End Function
'未知の貯水量Vを求める(2点の直線の傾きと既知のHを使って)
Function ValueBetween2(V1, H1, V2, H2, H_measured)
ValueBetween2 = V1 + ((V2 - V1) / (H2 - H1)) * (H_measured - H1)
End Function