Use Excel to implement the calculation of strings

zhaozj2021-02-16  56

How to calculate a given string calculation expression, such as the value of "1 2 * 3-4 / 5 6 ^ 7"? The author can enter such an expression when using Excel2002, and the output is the calculation result, so a function is written to share with you.

'Reference microsoft excel 10.0 object library (OR OTHER VERSION)' add a textbox and a commandbutton to form1Function result (ByVal x As String) Dim myobj As ObjectSet myobj = CreateObject ( "excel.sheet") Set myobj = myobj.Application.ActiveWorkbook .Activesheetmyobj.Range ("A1"). Formula = "=" & x 'Result = myobj.Range ("a1"). Valueif Err.Number> 0 Then msgBox err.descriptionSet myobj = Nothing

END FUNCTION

PRIVATE SUB Command1_Click () DIM X AS Stringx = Text1.TextMsgbox X & "=" & Result (x) End Sub

Private suform_load () text1.text = "3 * 9 ^ 10-21 * 256 ^ a" error expressions, returns an error message. You can change it into a legal expression and press Click Command Reparse Sub

转载请注明原文地址:https://www.9cbs.com/read-23732.html

New Post(0)