creating a small array from spreadsheet values/counts
Hi tigpup,
Try this function. The two ranges are inputted as one, so the left
column has to be the frequencies and the right column the values as in
your example data.
Public Function MEDIAN2(DataArray As Range) As Single
Application.Volatile
Dim MedianArray() As Single
Dim I As Integer
Dim J As Integer
Dim K As Long
Dim iArrayRows As Long
Dim iDataRows As Long
Dim iSheetRows As Long
iSheetRows = DataArray.Rows.Count
iArrayRows = WorksheetFunction.Sum(DataArray. _
Range(Cells(1, 1), Cells(iSheetRows, 1)))
ReDim Preserve MedianArray(1 To iArrayRows)
For I = 1 To iSheetRows
For J = 1 To DataArray.Cells(I, 1).Value
K = K + 1
MedianArray(K) = DataArray.Cells(I, 2).Value
Next J
Next I
If (UBound(MedianArray) Mod 2) = 0 Then
Let MEDIAN2 = (MedianArray(UBound(MedianArray) / 2) _
+ MedianArray(UBound(MedianArray) / 2 + 1)) / 2
Else: Let MEDIAN2 = MedianArray(UBound(MedianArray) / 2 + 0.5)
End If
End Function
I couldn't call it MEDIAN, that name clashed with the standard MEDIAN
function.
Ken Johnson
|