利用eva l函数突破eva luate的限制
责任编辑:hylng    浏览:6842次    时间: 2012-12-31 23:19:12      

免职声明:本网站为公益性网站,部分信息来自网络,如果涉及贵网站的知识产权,请及时反馈,我们承诺第一时间删除!

This website is a public welfare website, part of the information from the Internet, if it involves the intellectual property rights of your website, please timely feedback, we promise to delete the first time.

电话Tel: 19550540085: QQ号: 929496072 or 邮箱Email: Lng@vip.qq.com

摘要:在Excel中,有一个eva luate方法,来获得给定运算式的结果,我们常常在VBA是借用它,但使用eva luate方法时,给定的字符串如果超过255个字符,则会提示类型不匹配,如: Sub text1() Dim x As String x = "1+2+3+4+5" MsgBox Application.eva luate(x) x = "((2...

分享到:

在Excel中,有一个eva luate方法,来获得给定运算式的结果,我们常常在VBA是借用它,但使用eva luate方法时,给定的字符串如果超过255个字符,则会提示类型不匹配,如:

Sub text1()
    Dim x As String
    x = "1+2+3+4+5"
    MsgBox Application.eva luate(x)
    x = "((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+4*(0.0+0.0)+3*0.0+0.0+(2.4+3.9+2.8+1.3)+2+5+5*2+2+5+5*2-((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+4*(0.0+0.0)+3*0.0+0.0+(2.4+3.9+2.8+1.3)+2+5+5*2+2+5+5*2* ((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+4*(0.0+0.0)+3*0.0+0.0+(2.4+3.9+2.8+1.3)+2+5+5*2+2+5+5*2"
    MsgBox Application.eva luate(x)
End Sub

这时我们可以借用eva l函数来获得上面的结果。

Function Autocal1(Cell)
With CreateObject("MSScriptControl.ScriptControl")
.Language = "vbscript"
Autocal1 = .eva l(Cell)
End With
End Function

Function Autocal2(Cell)
With CreateObject("Access.Application")
Autocal2 = .eva l(Cell)
End With
End Function

Sub test()
    Dim x As String
    x = "((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+4*(0.0+0.0)+3*0.0+0.0+(2.4+3.9+2.8+1.3)+2+5+5*2+2+5+5*2-((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+4*(0.0+0.0)+3*0.0+0.0+(2.4+3.9+2.8+1.3)+2+5+5*2+2+5+5*2* ((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+((2.4+3.9+2.8+1.3)+(4.7+2.8)+(6.3+0.7+9.1+5.1+3.2+0.4+4.2+0.6))*0.5+0.0+2*0.0+4*(0.0+0.0)+3*0.0+0.0+4*(0.0+0.0)+3*0.0+0.0+(2.4+3.9+2.8+1.3)+2+5+5*2+2+5+5*2"
    MsgBox Autocal1(x)
    MsgBox Autocal2(x)
    [A1] = x
    MsgBox Autocal1([A1])
    MsgBox Autocal2([A1])
End Sub

上面两个自定义函数,还可以在工作表中直接使用,如:

=Autocal1(A1)
=Autocal2(A1)

】【打印繁体】【投稿】 【收藏】 【推荐】 【举报】 【评论】 【关闭】【返回顶部