Formulas and functions

%3CLINGO-SUB%20id%3D%22lingo-sub-2765157%22%20slang%3D%22en-US%22%3EFormulas%20and%20functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2765157%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIs%20there%20a%20way%20to%20calculate%20a%20value%20displayed%20in%20one%20cell%20in%20another%20cell%26nbsp%3B%20%26nbsp%3B%20ie%3A%20if%20I%20had%20a%20value%20of%203%2F10%20expressed%20as%20text%20in%20one%20cell%2C%20is%20there%20a%20way%20to%20calculate%20this%20value%20as%20a%20decimal%20in%20an%20adjacent%20cell%20ie%3A%200.3%3F%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3ENick%20Dodd%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2765157%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2765205%22%20slang%3D%22en-US%22%3ERe%3A%20Formulas%20and%20functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2765205%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1160387%22%20target%3D%22_blank%22%3E%40Nick_Dodd%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you're%20willing%20to%20use%20VBA%3A%3C%2FP%3E%0A%3CP%3EPress%20Alt%2BF11%20to%20activate%20the%20Visual%20Basic%20Editor.%3C%2FP%3E%0A%3CP%3ESelect%20Insert%20%26gt%3B%20Module.%3C%2FP%3E%0A%3CP%3ECopy%20the%20following%20function%20into%20the%20code%20window%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EFunction%20Eval(s%20As%20String)%0A%20%20%20%20Eval%20%3D%20Evaluate(s)%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EClose%20the%20Visual%20Basic%20Editor.%3C%2FP%3E%0A%3CP%3EUse%20like%20this%20in%20a%20cell%20formula%20to%20evaluate%20the%20expression%20in%20A2.%20The%20formula%20can%20be%20anywhere.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DEval(A2)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ESave%20the%20workbook%20as%20a%20macro-enabled%20workbook%20(.xlsm)%20and%20make%20sure%20that%20you%20allow%20macros%20when%20you%20open%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlternatively%2C%20you%20can%20use%20a%20defined%20name%3B%20this%20will%20only%20work%20in%20the%20cell%20to%20the%20right%20of%20the%20text%20string.%3C%2FP%3E%0A%3CP%3ESo%20let's%20say%20you%20have%20a%20text%20expression%20in%20A2.%3C%2FP%3E%0A%3CP%3ESelect%20B2.%3C%2FP%3E%0A%3CP%3EOn%20the%20Formulas%20tab%20of%20the%20ribbon%2C%20click%20Define%20Name...%3C%2FP%3E%0A%3CP%3EEnter%20%3CSTRONG%3EEval%3C%2FSTRONG%3E%20in%20the%20Name%20box%2C%20and%20enter%20the%20formula%20%3CSTRONG%3E%3DEVALUATE(A2)%3C%2FSTRONG%3E%20in%20the%20Refers%20To%20box%2C%20then%20click%20OK.%3C%2FP%3E%0A%3CP%3EEnter%20the%20following%20formula%20in%20B2%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DEval%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ENote%20that%20there%20is%20no%20reference%20to%20A2%20in%20the%20cell%20formula%3B%20the%20reference%20is%20in%20the%20definition%20of%20Eval.%3C%2FP%3E%0A%3CP%3EIf%20you%20enter%20%3DEval%20in%20cell%20D6%2C%20it%20will%20refer%20to%20C6%2C%20etc.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2765309%22%20slang%3D%22en-US%22%3ERe%3A%20Formulas%20and%20functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2765309%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1160387%22%20target%3D%22_blank%22%3E%40Nick_Dodd%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYour%20example%2C%20Nick%2C%20is%20VERY%20specific.%20So%20I'm%20going%20to%20assume%20that%20you're%20wanting%20this%20to%20work%20with%20what%20we%20might%20call%20traditional%20fractional%20notation.%20IF%20that's%20the%20case%2C%20then%20here's%20a%20formula%20that%20doesn't%20require%20VBA%20to%20work.%26nbsp%3B%3C%2FP%3E%3CP%3E%3DVALUE(LEFT(A1%2CFIND(%22%2F%22%2CA1)-1))%2FVALUE(RIGHT(A1%2CLEN(A1)-FIND(%22%2F%22%2CA1)))%3C%2FP%3E%3CP%3EThis%20finds%20the%20value%20of%20whatever%20text%20in%20cell%20A1%20precedes%20the%20%22%2F%22%20character%2C%20and%20divides%20that%20by%20the%20value%20of%20whatever%20text%20follows%20the%20%22%2F%22%20character.%20It%20uses%20the%20text%20manipulation%20functions%20LEFT%20and%20RIGHT%20to%20determine%20select%20out%20the%20text%2C%20FIND%20and%20LEN%20to%20count%20the%20characters%20as%20needed%2C%20and%20then%20VALUE%20to%20convert%20text%20to%20value.%3C%2FP%3E%3CP%3EAs%20this%20image%20shows%2C%20because%20it's%20dependent%20on%20the%20location%20of%20the%20%22%2F%22%20character%2C%20it%20will%20work%20with%20more%20complex%20fractions%20as%20well.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mathetes_0-1632073180554.png%22%20style%3D%22width%3A%20655px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311451i405B4D3BD9502D99%2Fimage-dimensions%2F655x144%3Fv%3Dv2%22%20width%3D%22655%22%20height%3D%22144%22%20role%3D%22button%22%20title%3D%22mathetes_0-1632073180554.png%22%20alt%3D%22mathetes_0-1632073180554.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20if%20you%20want%20a%20function%20to%20work%20with%20other%20kinds%20of%20numbers%20in%20text%2C%20then%20let%20me%20invite%20you%20to%20be%20a%20bit%20more%20expansive%20on%20your%20description%20of%20what%20you're%20seeking%20to%20accomplish.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2786937%22%20slang%3D%22en-US%22%3ERe%3A%20Formulas%20and%20functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2786937%22%20slang%3D%22en-US%22%3EThanks%20for%20your%20response.%20I'm%20not%20able%20to%20try%20it%20this%20week%20as%20I%20am%20on%20holiday%2C%20but%20will%20give%20it%20a%20whirl%20next%20week.%3CBR%20%2F%3EYours%3CBR%20%2F%3ENick%20Dodd%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2787073%22%20slang%3D%22en-US%22%3ERe%3A%20Formulas%20and%20functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2787073%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1160387%22%20target%3D%22_blank%22%3E%40Nick_Dodd%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20funny%20and%20crazy%20too.%3CBR%20%2F%3EUsing%20FILTERXML()%20function%20you%20will%20return%20a%20matrix%20containing%20the%20numbers%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DFILTERXML(%22%3CX%3E%3CY%3E%22%26amp%3BSUBSTITUTE(K4%2C%22%2F%22%2C%22%3C%2FY%3E%3CY%3E%22)%26amp%3B%22%3C%2FY%3E%3C%2FX%3E%22%2C%22%2F%2Fy%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EThen%20you%20just%20need%20to%20get%20the%20first%20element%20and%20divide%20by%20the%20last%20element%3CBR%20%2F%3EINDEX(PreviousFormula%2C1)%2FINDEX(PreviousFormula%2C2)%3CBR%20%2F%3EAs%20it%20envolves%20a%20lot%20of%20effort%2C%20would%20be%20a%20good%20idea%20you%20use%20a%20more%20modern%20method%20like%20LET()%20function%2C%20so%20you%20can%20create%20a%20more%20elaborate%20example.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi     Is there a way to calculate a value displayed in one cell in another cell    ie: if I had a value of 3/10 expressed as text in one cell, is there a way to calculate this value as a decimal in an adjacent cell ie: 0.3?

Thanks

Nick Dodd

6 Replies

@Nick_Dodd 

If you're willing to use VBA:

Press Alt+F11 to activate the Visual Basic Editor.

Select Insert > Module.

Copy the following function into the code window:

Function Eval(s As String)
    Eval = Evaluate(s)
End Function

Close the Visual Basic Editor.

Use like this in a cell formula to evaluate the expression in A2. The formula can be anywhere.

=Eval(A2)

Save the workbook as a macro-enabled workbook (.xlsm) and make sure that you allow macros when you open it.

 

Alternatively, you can use a defined name; this will only work in the cell to the right of the text string.

So let's say you have a text expression in A2.

Select B2.

On the Formulas tab of the ribbon, click Define Name...

Enter Eval in the Name box, and enter the formula =EVALUATE(A2) in the Refers To box, then click OK.

Enter the following formula in B2:

=Eval

Note that there is no reference to A2 in the cell formula; the reference is in the definition of Eval.

If you enter =Eval in cell D6, it will refer to C6, etc.

@Nick_Dodd 

 

Your example, Nick, is VERY specific. So I'm going to assume that you're wanting this to work with what we might call traditional fractional notation. IF that's the case, then here's a formula that doesn't require VBA to work. 

=VALUE(LEFT(A1,FIND("/",A1)-1))/VALUE(RIGHT(A1,LEN(A1)-FIND("/",A1)))

This finds the value of whatever text in cell A1 precedes the "/" character, and divides that by the value of whatever text follows the "/" character. It uses the text manipulation functions LEFT and RIGHT to determine the relevant text, FIND and LEN to count the characters as needed, and then VALUE to convert text to value.

As this image shows, because it's dependent on the location of the "/" character, it will work with more complex fractions as well.

mathetes_0-1632073180554.png

 

 

But if you want a function to work with other kinds of numbers in text, then let me invite you to be a bit more expansive on your description of what you're seeking to accomplish.

 

Thanks for your response. I'm not able to try it this week as I am on holiday, but will give it a whirl next week.
Yours
Nick Dodd

@Nick_Dodd 

 

This is funny and crazy too.
Using FILTERXML() function you will return a matrix containing the numbers

 

=FILTERXML("<x><y>"&SUBSTITUTE(K4,"/","</y><y>")&"</y></x>","//y")


Then you just need to get the first element and divide by the last element
INDEX(PreviousFormula,1)/INDEX(PreviousFormula,2)
As it envolves a lot of effort, would be a good idea you use a more modern method like LET() function, so you can create a more elaborate example.

@Nick_Dodd 

This is very much 'for interest' at the moment because it relies upon functionality currently only available under Microsoft 365 insider beta channel.  The formula is an alternative form of that presented by @Hans Vogelaar .

 

I created a defined name EVALUATEλ (I use the Greek letter λ from Insert/Symbol merely as a reminder as to the nature of the formula the name refers to) defined by

= LAMBDA(expr, EVALUATE(expr))

The worksheet formula is then

= EVALUATEλ(expression)

where 'expression' is currently an array of fractions as text but could be any other formula in text form, such as 'EXP(1)' which would evaluate to the math constant e.

 

Note: EVALUATE is an old XLM macro command and there is no assurance that it will be supported intefinitely.

 

To make it dynamic considering the values are on left column

=EVALUATE(INDIRECT(ADDRESS(ROW(),COLUMN()-1)))

 Find attachment