计算某些值包含"<"

时间:2014-04-14 21:29:01

标签: excel vba excel-vba

我需要计算一组测量的中值,其中在某些情况下测量值,并且在某些情况下,该值低于检测值(由“<”符号表示,后跟检测限,例如< 1 )

以下是我遇到的一些案例:

  

2.0; 3.0; < 1.0; 4.0 - >中位数= 2.5
    1.0;小于0.5; &lt; 0.5 - &gt;中位数= <0.5
    1.0; 1.0;小于0.5; &lt; 0.5 - &gt;中位数= <0.75

我对excel VBA中的这一点感到有点难过 如何使用具有“&lt;”的值进行数学运算签署,同时仍然跟踪“&lt;”?

非常感谢任何意见 - 谢谢!

1 个答案:

答案 0 :(得分:2)

这是我使用的东西:

Public Function DoAvg(rng As Range)
  DoAvg = Parse(rng, "Average")
End Function

Public Function DoMedian(rng As Range)
  DoMedian = Parse(rng, "Median")
End Function


'This does the work...
Private Function Parse(rng As Range, CalcType As String)

Dim rv, arr() As Single, mods As String, i As Long
Dim c As Range
Dim tmp, m

  For Each c In rng.Cells
    tmp = Replace(Trim(c.Value), " ", "")
    If tmp Like "<*" Or tmp Like ">*" Then
        m = Left(tmp, 1)
        If Not InStr(mods, m) > 0 Then mods = mods & m
        tmp = Right(tmp, Len(tmp) - 1)
    End If

    If IsNumeric(tmp) And tmp <> "" Then
      i = i + 1
      ReDim Preserve arr(1 To i)
      arr(i) = tmp
    End If
  Next c

  If i > 1 Then
      rv = CallByName(Application.WorksheetFunction, CalcType, VbGet, arr)
      Parse = IIf(mods <> "", mods, "") & rv
  Else
      Parse = ""
  End if

End Function