Stephen Forero
asked on
Calculate Standard Deviation of portion of Array
Hi Guys,
I have a 3 column array that say is around 100 elements long.
I'm trying to using the worksheet.function to calculate the STDDEV, but only of a piece of the array.
FYI, doing all this in VBA
Like
'stddevArray(3, a) = application.WorksheetFunct ion.StDev( "item1 in array:item2 in array")
I understand the parameter has to be the range of stddev. Anyone know the quick and easy code for starting point and ending point in the array?
Thanks!!
I have a 3 column array that say is around 100 elements long.
I'm trying to using the worksheet.function to calculate the STDDEV, but only of a piece of the array.
FYI, doing all this in VBA
Like
'stddevArray(3, a) = application.WorksheetFunct
I understand the parameter has to be the range of stddev. Anyone know the quick and easy code for starting point and ending point in the array?
Thanks!!
ASKER
array would be 3 columns by 100 rows in VBA
say I want the standard deviation of 2nd column, 5th row through 10th row.
will your formula above handle that?
say I want the standard deviation of 2nd column, 5th row through 10th row.
will your formula above handle that?
Give it a try and see
MsgBox Application.StDev(Applicat ion.Index( v, Evaluate("Row(5:10)"), 2))
MsgBox Application.StDev(Applicat
ASKER
I get type mismatch
How did you declare your array? Can you post a sample workbook?
ASKER
the array is made from the following code attached:
the data is
dec-09 -3.54%
jan-10 -11.72
feb-10 7.45
and so on.
i tried this
MsgBox Application.StDev(Applicat ion.Index( stddevArra y, Evaluate("Row(5:10)"), 2))
the data is
dec-09 -3.54%
jan-10 -11.72
feb-10 7.45
and so on.
i tried this
MsgBox Application.StDev(Applicat
Sub RollingSD()
Dim stddevArray() As Variant
Dim lastRowData As Integer
Dim a As Integer 'rows of Vami
Dim x As Integer 'rows of original data
a = 1
Sheets("CData").Activate
lastRowData = Range("B65536").End(xlUp).row
ReDim stddevArray(1 To 3, 1 To a)
For x = 2 To lastRowData
ReDim Preserve stddevArray(1 To 3, 1 To a)
stddevArray(1, a) = Cells(x, 1).Value
stddevArray(2, a) = Cells(x, 2).Value
a = a + 1
Next x
Not sure, a sample workbook would help. A quick test for me worked.
ASKER
the data is from tab "CDATA"
peer.xlsm
peer.xlsm
OK, your array was the wrong way round, but in any case you can populate your array more quickly this way. Worked for me on a mock-up:
Sub RollingSD()
Dim stddevArray
Dim lastRowData As Integer
Dim a As Integer 'rows of Vami
Dim x As Integer 'rows of original data
Sheets("CData").Activate
stddevArray = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
MsgBox Application.StDev(Application.Index(stddevArray, Evaluate("Row(5:10)"), 2))
End Sub
Btw, didn't see your workbook so have not tested on that.
ASKER
Thank you so much Stephen. It does work... however I have one new problem with this.
the row to evaluate will be variable, so the section
Evaluate("Row(5:10)")
needs to be like Evaluate("Row(x:t)")
suggestions... and again I'm sorry for not specifying this earlier
the row to evaluate will be variable, so the section
Evaluate("Row(5:10)")
needs to be like Evaluate("Row(x:t)")
suggestions... and again I'm sorry for not specifying this earlier
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
awesome thanks!!! you guys make me look like a genius!!!
ha
ha
Remember my cut if you get a pay rise. I know where you live ...
ASKER
DONE!
e.g. this would give you stdev of 4th to 6th element in first column (where v is array):
MsgBox Application.StDev(Applicat