利用EXCEL 實現字符串的計算

發表于:2007-07-01來源:作者:點擊數: 標簽:
如何計算給定的字符串計算表達式,如“ 1+2*3-4/5+ 6^7” 的值?筆者在使用EXCEL2002 時發現在單元格中可以輸入此類表達式,輸出的則是計算結果,所以寫了一個函數,與大家共享。 引用microsoft excel 10.0 object library( OR OTHER VERSION) add a textbox

     如何計算給定的字符串計算表達式,如“ 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


原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97