Thursday, June 14, 2007

Excel: Visual Basic; mean value

Simple code in Microsoft VBA as a function in Excel. This function takes a range (x) and calculates mean value of the number lower than 0.
Function mymeanLr(x)
n = x.Count
ReDim xx(n + 1)
Dim ind As Integer
ind = 0
For i = 1 To n
If x(i) < 0 Then
xx(i - 1) = x(i)
ind = ind + 1
End If
Next i
mymeanLr = WorksheetFunction.Average(xx)
End Function