Here is a code for DMEDIAN function I wrote that works exactly with similar parameters such as other domain functions. I wrote this in Excel 2010, hope this helps.
Function DMEDIAN(Database, Field, Criteria)
On Error GoTo ErrorHandler
ReDim dati(Database.Rows.Count)
ReDim crtable(rcount, ccount)
ccount = Criteria.Columns.Count
rcount = Criteria.Rows.Count
dccount = Database.Columns.Count
drcount = Database.Rows.Count
'Find the Field index in Database
For i = 1 To dccount
If Database(1, i) = Field Then
iField = i
End If
Next i
cdati = 0
For i = 2 To drcount
For a = 2 To rcount
ok = True
bAtleastOneMatch = False
For j = 1 To dccount
If Database(i, j) = Empty Then
Exit For
End If
fNum = False
If IsNumeric(Database(i, j)) Then
fNum = True
End If
fdate = False
If IsDate(Database(i, j)) Then
fdate = True
End If
For b = 1 To ccount
If Criteria(a, b) <> Empty Then
If Criteria(1, b) = Database(1, j) Then
bAtleastOneMatch = True
Select Case Left(Criteria(a, b), 1)
Case "="
If fNum Then
ddata = CLng(Mid(Criteria(a, b), 2))
ElseIf fdate Then
ddata = CDate(Mid(Criteria(a, b), 2))
Else
ddata = Mid(Criteria(a, b), 2)
End If
If Database(i, j) <> ddata Then
ok = False
Exit For
End If
Case ">"
If Left(Criteria(a, b), 2) = ">=" Then
If fNum Then
ddata = CLng(Mid(Criteria(a, b), 3))
ElseIf fdate Then
ddata = CDate(Mid(Criteria(a, b), 3))
Else
ddata = Mid(Criteria(a, b), 3)
End If
If Database(i, j) < ddata Then
ok = False
Exit For
End If
Else
If fNum Then
ddata = CLng(Mid(Criteria(a, b), 2))
ElseIf fdate Then
ddata = CDate(Mid(Criteria(a, b), 2))
Else
ddata = Mid(Criteria(a, b), 2)
End If
If Database(i, j) <= ddata Then
ok = False
Exit For
End If
End If
Case "<"
If Left(Criteria(a, b), 2) = "<>" Then
If fNum Then
ddata = CLng(Mid(Criteria(a, b), 3))
ElseIf fdate Then
ddata = CDate(Mid(Criteria(a, b), 3))
Else
ddata = Mid(Criteria(a, b), 3)
End If
If Database(i, j) = ddata Then
ok = False
Exit For
End If
ElseIf Left(Criteria(a, b), 2) = "<=" Then
If fNum Then
ddata = CLng(Mid(Criteria(a, b), 3))
ElseIf fdate Then
ddata = CDate(Mid(Criteria(a, b), 3))
Else
ddata = Mid(Criteria(a, b), 3)
End If
If Database(i, j) > ddata Then
ok = False
Exit For
End If
Else
If fNum Then
ddata = CLng(Mid(Criteria(a, b), 2))
ElseIf fdate Then
ddata = CDate(Mid(Criteria(a, b), 2))
Else
ddata = Mid(Criteria(a, b), 2)
End If
If Database(i, j) >= ddata Then
ok = False
Exit For
End If
End If
End Select
End If
End If
Next b
If ok = False Then
Exit For
End If
Next j
If ok = True And bAtleastOneMatch = True Then 'Add the number for median calculation
cdati = cdati + 1
dati(cdati) = Database(i, iField)
Exit For
End If
Next a
Next i
'Sort the dati array in ascending order before calculating the Median
For i = 2 To cdati
For j = 1 To i - 1
If dati(j) > dati(i) Then
Tmp = dati(j)
dati(j) = dati(i)
dati(i) = Tmp
End If
Next j
Next i
'Calculate Median
If cdati Mod 2 = 1 Then
DMEDIAN = dati(Int(cdati / 2) + 1)
Else
DMEDIAN = (dati(cdati / 2) + dati(cdati / 2 + 1)) / 2
End If
Exit Function
ErrorHandler:
MsgBox "Error at DRow = " + i + ", DCol = " + j + ", CRow = " + a + ", CCol = " + b + ", Database = " + Database(i, j) + ", Criteria = " + Criteria(a, b)
End Function