用EXCEL自定義函數(shù)計(jì)算結(jié)果或得到值的計(jì)算表達(dá)式

字號(hào):

有許多朋友想知道計(jì)算表達(dá)式結(jié)果的方法,我自己做了一個(gè),供大家參考,方法如下:
    打開excel,打開“工具_(dá)宏_Visual Basic編輯器”,在左側(cè)的工程資源管理器中本文件名稱上點(diǎn)擊右鍵,選擇“插入_模塊”,在添加的模塊的空白代碼窗口中粘貼如下代碼:
    Function YCH(JSS, Optional x) ’返回計(jì)算公式的值或值的計(jì)算公式
    Dim S%, E%
    Dim JS As String
    If JSS = "" Then
    YCH = ""
    Else
    If IsMissing(x) Then ’返回計(jì)算公式的值
    If Left(JSS.Value, 1) = "=" Then
    JSS = Mid(JSS, 2)
    End If
    Do Until InStr(1, JSS, "[") = 0
    S = InStr(1, JSS, "[")
    E = InStr(1, JSS, "]")
    JSS = Left(JSS, S - 1) & Mid(JSS, E + 1)
    Loop
    YCH = Evaluate("=" & JSS)
    ElseIf x = 2 Then ’返回值的計(jì)算公式或可計(jì)算的表達(dá)式或值本身
    If JSS.HasFormula = True Then
    YCH = Mid(JSS.Formula, 2)
    Else
    If IsNumeric(Evaluate(JSS.Value)) = True Then
    YCH = JSS.Value
    Else
    JS = JSS.Value
    Do Until InStr(1, JSS, "[") = 0
    S = InStr(1, JSS, "[")
    E = InStr(1, JSS, "]")
    JSS = Left(JSS, S - 1) & Mid(JSS, E + 1)
    Loop
    If IsNumeric(JSS) = True Or IsNumeric(Evaluate(JSS)) = True Then
    YCH = JS
    End If
    End If
    End If
    End If
    End If
    End Function