2006-12-21

Excel VBAの解答例

今日はパワーポイントを勉強しますが、その前に前回のレポート課題である、Visual Basicを使った平均、分散を求めるプログラムの解答例を示します。
Sub 解答例()
Dim x()

i = 1
Do While Cells(i, 1) <> ""
    i = i + 1
Loop
kosu = i - 1
Cells(1, 2) = "個数"
Cells(1, 3) = kosu
ReDim x(kosu)

For i = 1 To kosu
    x(i) = Cells(i, 1)
Next i

wa = 0
For i = 1 To kosu
    wa = wa + x(i)
Next i
Cells(2, 2) = "合計"
Cells(2, 3) = wa

heikin = wa / kosu
Cells(3, 2) = "平均"
Cells(3, 3) = heikin

wa = 0
For i = 1 To kosu
    wa = wa + (x(i) - heikin) ^ 2
Next i
Cells(4, 2) = "分散"
Cells(4, 3) = wa / kosu

End Sub

0 件のコメント: