利用EXCEL 实现字符串的计算

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 northwolves 的 blog

     如何计算给定的字符串计算表达式,如“ 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

本文关键:字符串计算 EXCEL
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top