# Thread: How to calculate Standard Deviation

1. ## How to calculate Standard Deviation

Can someone help me, Iam trying to calculate the standard Deviation.
here is part of the code that I am using.
Dim dSum As Double
Dim lCol As Long, lRow As Long

Dim mean As Double
Dim mean2 As Double

Dim sdA As Double
lCol = 4 ' The column you want to sum
With Grid1
For lRow = .FixedRows To .Rows - 1 ' Process all rows except fixed ones
If .TextMatrix(lRow, lCol) = "" Then
GoTo final
Else

dSum = dSum + CDbl(.TextMatrix(lRow, lCol)) 'Suma la la ultima columna a la anterior
average1 = Round(dSum / Valor, 2)

txtAvarage = average1
End If
Next lRow
End With
final:
Call calculateSD

End Sub
Private Sub calculateSD()
Dim lrows As Integer
Dim sd As Double
With Grid1
For lrows = .FixedRows To .Rows - 1
If .TextMatrix(lrows, 4) = "" Then
Exit Sub
Else
sd = (CDbl(.TextMatrix(lrows, 4)) - average1) ^ 2

sd = sd + (CDbl(.TextMatrix(lrows, 4)) - average1) ^ 2
txtSD = Sqr(sd)

End If

Next
End With
End Sub

2. ## Re: How to calculate Standard Deviation

Try;

Code:
```Private Sub Form_Load()

Dim MyArray As Variant
Dim x#, sx#, sx2#, cnt#

AutoRedraw = True

'set up demodata
MyArray = Array(Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd())

'get/ asssign data
For i = 0 To UBound(MyArray)
cnt = cnt + 1
x = MyArray(i)
Print x
sx = sx + x
sx2 = sx2 + x * x
Next

'report results
Print "average: " & sx / cnt
Print "sd: " & Sqr((cnt * sx2 - (sx * sx)) / (cnt * (cnt - 1)))

Height = (Height - ScaleHeight) + CurrentY + TextHeight("I")

End Sub```

3. ## Re: How to calculate Standard Deviation

http://en.wikipedia.org/wiki/Standard_deviation
Perhaps you should review this again before you write code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured

Click Here to Expand Forum to Full Width