Median Module Error

dimsum1

New Member
Joined
Aug 30, 2014
Messages
6
I was hoping someone could help out with a median module we are trying to get to work.
It fails at the rs.MoveLast step if the particular grouped values are all 0 values. As long as the result field has a value the code works perfectly. Anyone have a good way to get around this? In other words I want the median formula to ignore the calculation or put something like a 0 and move on to the next group. - Thanks

Code:
Function MedianF(pTable As String, pfield As String, Optional pgroup As String) As Single

Dim rs       As recordSet
Dim strSQL   As String
Dim n        As Integer
Dim sglHold  As Single
If Len(pgroup) > 0 Then
    strSQL = "SELECT " & pfield & " from " & pTable & " WHERE analyte= '" & pgroup & "' and " & pfield & ">0 Order by " & pfield & ";"
Else
    strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0 Order by " & pfield & ";"
End If
Set rs = CurrentDb.OpenRecordset(strSQL)
    rs.MoveLast
    n = rs.RecordCount
    rs.Move -Int(n / 2)
    
    If n Mod 2 = 1 Then 'odd number of elements
       MedianF = rs(pfield)
    Else                'even number of elements
       sglHold = rs(pfield)
       rs.MoveNext
       sglHold = sglHold + rs(pfield)
       MedianF = sglHold / 2
    End If
    rs.Close
End Function
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
rs.Move -In(n/2) is a command.

Check that records are returned in the first place:

Code:
If rs.BOF and rs.EOF Then
 MsgBox "No Records Returned"
End If

expression.Move(Rows, StartBookmark)
expression A variable that represents a Recordset object.
Parameters


NameRequired/OptionalData TypeDescription
RowsRequiredLongThe number of rows the position will move. If rows is greater than 0, the position is moved forward (toward the end of the file). If rows is less than 0, the position is moved backward (toward the beginning of the file).
StartBookmarkOptionalVariantA value identifying a bookmark. If you specify startbookmark, the move begins relative to this bookmark. Otherwise, Move begins from the current record.

<tbody>
</tbody>
 
Upvote 0
Thanks guys I found alternative code that works but I'll try your suggestions and post back with the results.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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