Statistical function

BernieRS

New Member
Joined
Jan 30, 2004
Messages
39
Is there a way to construct the WorksheetFuction call using any passed statistic name, rather than only allowing specific ones as in the following working example? Thanks in advance.

Code:
Function CalcStat(stat As String, col As Integer)
'
' Calculate statistics (from hidden Data sheet values)
'
    On Error Resume Next
    If col = 0 Then 'Entire Data sheet (all data sets)
        firstcell = "$A$1"
        lastrow = Worksheets("Data").UsedRange.Rows.Count
        lastcol = Worksheets("Data").UsedRange.Columns.Count
        lastcell = Worksheets("Data").UsedRange.Cells(lastrow, lastcol).Address
    Else 'One column (one data set)
        firstcell = Worksheets("Data").Cells(1, col).Address
        lastrow = Worksheets("Data").Cells(Rows.Count, col).End(xlUp).row
        lastcell = Worksheets("Data").UsedRange.Cells(lastrow, col).Address
    End If
    datarng = firstcell & ":" & lastcell
    Select Case stat
    Case "Min"
        CalcStat = WorksheetFunction.Min(Worksheets("Data").Range(datarng))
    Case "Max"
        CalcStat = WorksheetFunction.Max(Worksheets("Data").Range(datarng))
    Case "Average"
        CalcStat = WorksheetFunction.Average(Worksheets("Data").Range(datarng))
    Case "StDev"
        CalcStat = WorksheetFunction.StDev(Worksheets("Data").Range(datarng))
    End Select

    CalcStat = Round(CalcStat, 1)

End Function
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

BernieRS

New Member
Joined
Jan 30, 2004
Messages
39
I suspected not. I tried various Set constructions to no avail. Thanks.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,030
Messages
5,835,038
Members
430,333
Latest member
jaypatel1298

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
Top