|
“算式求值”自定义函数:SEVALUATE
说明:
1、算式中允许出现×、÷符号;
2、算式中允许出现中英文注释内容(但每一项注释内容需要以成对的中括号[]将其包含起来)
3、本自定义函数只能在32位的EXCEL软件中使用,不能用于64位EXCEL。
- Const strPattern As String = "(\[[^\][]*\])+"
- Function SEVALUATE(ByVal Expression As String)
- Dim regEx As Object, x As Object
- Set x = CreateObject("ScriptControl")
- x.Language = "VbScript"
- Set regEx = CreateObject("VbScript.RegExp")
- regEx.Pattern = strPattern
- regEx.IgnoreCase = True
- regEx.Global = True
- On Error Resume Next '
- If Expression = "" Then
- SEVALUATE = ""
- Else
- SEVALUATE = Replace(Replace(regEx.Replace(Expression, " "), "(", "("), ")", ")")
- SEVALUATE = Replace(Replace(SEVALUATE, "×", "*"), "÷", "/")
- SEVALUATE = Replace(Replace(SEVALUATE, "+", "+"), "-", "-")
- SEVALUATE = x.EVAL(SEVALUATE)
- If Not IsNumeric(SEVALUATE) Then SEVALUATE = "错误:" & SEVALUATE
- End If
- End Function
复制代码
|
评分
-
1
查看全部评分
-
|