Compute string content

  • Thread starter Thread starter jfkaufeler
  • Start date Start date
J

jfkaufeler

How can I compute in a cell an expression string such as "sqrt(2)*(5-2)" ?
 
Put this formula in the cell:
=SQRT(2)*(5-2)
and you should get a result like 4.242641
 
How can I compute in a cell an expression string such as "sqrt(2)*(5-2)" ?

Not sure exactly what you mean.

First, do you actually have the quotes ("...") in the cell; or do you
have just the text without the quotes?

In either case, the "obvious" answer is: remove the quotes ("..."),
if they are present, and insert an equal sign (=) on the left. In
other words, manually turn the text into a formula.

But if what you mean is: you an expression as text in a cell, and in
another cell, you would like to evaluate the text as an Excel formula,
perhaps the following UDF would be helpful:

function doit(rng as Range)
s = replace(rng.value, chr(34), "")
doit = evaluate(s)
end function

Usage: If A1 contains the expression as text:

=doit(A1)

Note: The Replace() function call is needed only if you might have
explicit quotes in the string expression. If not, remove the "s="
line and "s" with rng.value in the Evaluate() function call.

Use alt+F11 and Insert>Module to enter the text as in the VB Editor.
 
Function EvalCell(RefCell As String)
Application.Volatile
EvalCell = Evaluate(RefCell)
End Function

Get rid of the quote marks then in a cell enter =evalcell(cellref)

Your example returns 4.242640687


Gord Dibben MS Excel MVP
 
Thank you for your prompt anwer. I didn't formulate my problem correctly.
What is in the cell is an expression string which was itself computed from
other cells (e.g. concatenation of different cell content (values and
operators).
Regards,
 
Many thanks, it looks to answer my question but I didn't have time to try.
Regards,
 
Many thanks, it may solve my problem but I have not tried yet, regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top