Rather than upsetting people by posting what would be an even longer
message and occupying bandwidth unnecessarily, I have uploaded a zip
file to my business website. The file contains the Class as a .cls
file and a small database that demonstrates its use in a report.
Note that you do have to change .BucketSize from its default to get
the median of five or less values.
After importing the Class into your Application, your Median function
would look something like this:
Function DoMedian (varA, varB, varC, varD, varE) As Variant
' You can declare everything in the declaration with numeric data
' types if you know that they are all going to be numeric
Dim objStats As CumStats
Set objStats = New CumStats
objStats.BucketSize = 1
objStats.AddItems (varA, varB, varC, varD, varE)
DoMedian = objStats.Median
set objStats = Nothing
End Function
The link is
http://www.parish-data.com/data/cumstats.zip
The externally visible Methods of the Class are as follows:
AddItem(VarValue as Variant) - Adds an value to the internal list of
values of which the Median, first Decile, or whatever boundary value
you want will be returned. If you pass AddItem a Null it will simply
be ignored. The data type (Date, Integer, String, etc.) of the first
value added to the list is recorded. Subsequent values added must be
of the same type, or you will get an error box and the new item will
not be added.
AddItems(VarArray as Variant | VarValue1, VarValue2,....,VarValueN as
Variants) - Adds multiple values to the list. Accepts either an Array
containing multiple values or a list of values. This calls AddItem
internally, so it follows the same rules regarding data types.
Median - Returns a Variant containing the median value from the list.
This is written as a Method but can be regarded as a readonly
Property. If there are less than 2 * BucketSize (see below) total
values in the list, Median returns Null.
Slice(NSlices as Integer, SliceNumber as Integer) - Returns a Variant
containing the specified boundary value. This does quartiles, deciles,
or whatever you want - for the first quartile boundary value you would
invoke it as Slice(4,1). Median converts internally into Slice(2,1).
If the number of values in the list divided by NSlices is less than
than BucketSize (see below), Slice returns Null, as it does if
SliceNumber>= NSlices, or either argument is <1.
The externally visible Properties of the Class are:
Count - Returns a Long containing the number of values in the list.
BucketSize - Gets or Sets the integer number of values in a slice
below which Slice (or Median) will not return a value. This is
read/write and the default value is 3. The object of this is to avoid
reporting stupid statistics (like the 4th decile boundary of a list of
8 values!).
Let me know if you have any problems with it.
Peter,
Thanks, and yes, please provide the link.
--Steve
Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher