How to get Stdev of Range?

warlordusa

New Member
Joined
Jan 13, 2023
Messages
1
Hello all,

Below is apart of my code where I take a range of data, and try to find the avg, max, min, and now stdev. I am getting values for my avg max min, but nothing for my std, specifically this line of code:

Std = Application.WorksheetFunction.StDev_S(MyRange)

Any help would be appreciated.
Thanks




Code here:


Dim CurrentColumn As Integer
CurrentColumn = 5 '=5 because results start on column E

Dim LastColumn As Integer

'Find the last used Column
LastColumn = Sheet1.Cells(7, Columns.Count).End(xlToLeft).Column

'Loop to get all of the results and formats in each column

Do While CurrentColumn <= LastColumn

Dim MyRange As Range
Dim Avg As Double
Dim Min As Double
Dim Max As Double
Dim Std As Double
Dim AvgPlusStd As Double
Dim AvgMinStd As Double



Set MyRange = Range(Cells(7, CurrentColumn), Cells(LastDataRow, CurrentColumn))

'Put Average, Max, and Min In Table
'And now Stdev and +/- 3 Dev

On Error Resume Next ' This line ignores an error and goes to the next line of code. This is usefull when trying to take the

Avg = Application.WorksheetFunction.Average(MyRange)
Min = Application.WorksheetFunction.Min(MyRange)
Max = Application.WorksheetFunction.Max(MyRange)
Std = Application.WorksheetFunction.StDev_S(MyRange)
AvgPlusStd = Avg + (3 * Std)
AvgMinStd = Avg - (3 * Std)

If AvgMinStd < 0 Then
AvgMinStd = 0
End If


Sheet1.Cells(AvgRow, CurrentColumn).Value = Avg
Sheet1.Cells(MinRow, CurrentColumn).Value = Min
Sheet1.Cells(MaxRow, CurrentColumn).Value = Max
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe MyRange contains one or more error values?

If this is the case, StDev will return an error.

However, the error would be ignored, since you have On Error Resume Next. And std would remain unchanged with either its initialized value, that being 0, or previously assigned value.

First, I would suggest removing On Error Resume Next from your code.

Secondly, declare std as Variant instead of Long. This will allow it to store either a Double or error value.

Thirdly, use the std method of the Application object, instead of the WorksheetFunction object...

VBA Code:
Std = Application.StDev_S(MyRange)

This allows for a non-breaking error, for which you can test...

VBA Code:
If Not IsError(Std) Then
    'do something
Else
    'do something else
End If

Hope this helps!
 
Upvote 0
Some further suggestions...

1) You should declare all your variables. You can place the Option Explicit statement at the top of your module, which will force all variables to be declared, and help avoid possible errors (and headaches).

2) You should qualify all your range references, otherwise they'll refer to the active sheet instead of the intended sheet. So, for example, if the active sheet is a chart sheet instead of a worksheet, you'll get an error. So you can avoid this by doing it this way...

VBA Code:
'Find the last used Column
LastColumn = Sheet1.Cells(7, Sheet1.Columns.Count).End(xlToLeft).Column


and

VBA Code:
With Sheet1
    Set MyRange = .Range(.Cells(7, CurrentColumn), .Cells(LastDataRow, CurrentColumn))
End With

Notice the dots ( . ) referring to Sheet1.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top