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
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

BernieRS

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

Watch MrExcel Video

Forum statistics

Threads
1,122,809
Messages
5,598,202
Members
414,218
Latest member
speedbit

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