Custom Function for % Relative Standard Deviation

smpatty08

Board Regular
Joined
May 16, 2014
Messages
155
I am trying to create a custom function for %RSD. The formula for %RSD is the (Standard Deviation of a range/Average of the same range)*100. this is normally done in excel by using the STDEV and AVERAGE functions, but I would like a function the will do it by just selecting the range.
Code:
=RSD(A1:A6)

Here is the code I have so far, but I keep getting errors.
Code:
Function RSD(rng As Range)
Dim cell As Integer
For Each cell In rng
    If cell <> 0 And IsNumeric Then
        RSD = ((stdev(rng) / Average(rng)) * 100)
    Else
        MsgBox ("One or more cells in selected range is blank or not numeric.")
    End If
Next cell
End Function
I am familiar with VBA, but I have never made a custom function. Any help would be appreciated. THANKS!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi there

Ihave changed your code as follows - I think it works!:

Code:
Function RSD(rng As Range)
Dim cell As Range

For Each cell In rng
    If cell.Value <> 0 And IsNumeric(cell.Value) Then
        RSD = ((Application.WorksheetFunction.StDev(rng) / Application.WorksheetFunction.Average(rng)) * 100)
    Else
        MsgBox ("One or more cells in selected range is blank or not numeric.")
    End If
Next cell
End Function
 
Upvote 0
Hi.
I see some mistakes here :

Code:
Dim cell As Integer
For Each cell In rng

cell is defined as an integer but used as a Range in the For Each loop.
Try defining cell as a Range.

Code:
Dim cell As Range

The IsNumeric function is waiting a parameter.
stdev and average are worksheet functions, so you need to do WorksheetFunction.Function
You need to define the output of your function (integer, double, ...) like this :

Code:
Function functionname() As Double

These will normally remove the errors.

Also if I may, your function is doing too much calculation. You re-calculate your output for each cell.

Try this :

Code:
Function RSD(rng As Range) As Double
Dim cell As Range
For Each cell In rng
    If cell.Value = "" Or IsNumeric(cell) = False Then
MsgBox "One or more cells in selected range is blank or not numeric."
Exit Function
End If
        

Next cell
RSD = ((WorksheetFunction.stdev(rng) / WorksheetFunction.Average(rng)) * 100)
End Function
 
Last edited:
Upvote 0
You're welcome, although I would use LouisH's version because, as stated, my solution would unnecessarily process the entire range.
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,284
Members
448,885
Latest member
LokiSonic

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