Sub main1() Dim StartRow As Long, EndRow As Long ' 処理開始行,処理最終行 Dim r As Long ' カウンタ Dim FirstValue As Long, SecondValue As Long, SumOfValue As Long ' 値1,値2,値の合計 Dim ValueOfSquareRoot As Double ' 値の合計の平方根
Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("SQRT練習")
StartRow = 2 EndRow = 6
' 指定行のみ計算。逐一セルに代入する。計算ステップは細かくしている。虚数となる場合は一律-1をセルに代入。 With ws For r = StartRow To EndRow FirstValue = .Cells(r, 2).Value SecondValue = .Cells(r, 3).Value SumOfValue = FirstValue + SecondValue If SumOfValue > 0 Then ValueOfSquareRoot = Sqr(SumOfValue) Else ValueOfSquareRoot = -1 End If .Cells(r, 4).Value = ValueOfSquareRoot Next End With