利用EXCEL 实现字符串的计算

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

     如何计算给定的字符串计算表达式,如“ 1+2*3-4/5+ 6^7” 的值?笔者在使用EXCEL2002 时发现在单元格中可以输入此类表达式,输出的则是计算结果,所以写了一个函数,与大家共享。

 

'引用microsoft EXCEL 10.0 object library( OR OTHER VERSION)
' add a textbox and a commandbutton to form1
Function result(ByVal x As String)
Dim myobj As Object
Set myobj = CreateObject("EXCEL.sheet")
Set myobj = myobj.Application.ActiveWorkbook.ActiveSheet
myobj.Range("a1").Formula = "= " & x '
result = myobj.Range("a1").Value
If err.Number > 0 Then MsgBox err.Description
Set myobj = Nothing

End Function

Private Sub Command1_Click()
Dim x As String
x = Text1.Text
MsgBox x & "=" & result(x)
End Sub

Private Sub Form_Load()
Text1.Text = "3*9^10-21*256^a" '错误表达式,返回错误信息。你可以改成合法表达式再按单击COMMAND1
End Sub

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: vb
上一篇如何获得对窗体移动事件的处理的一个方法
下一篇利用API 实现字符串表达式的计算
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭