Excel - Execute formula stored as text
Dav
-
mouftie -
mouftie -
Hello,
In Excel, I would like to know if it is possible to execute a formula written in another cell as a simple string of characters within a cell.
This way, it would be possible to replace an endless "IF" formula (else IF, else IF, etc.) with a lookup in a matrix of formulas, depending on the case at hand.
For example - Let's assume a sheet that converts measurement units (Celsius to Fahrenheit, gallons to liters, etc.):
- A "Conversion" tab contains two editable cells: A1="Value to convert" and A2="Conversion to apply" and a non-editable cell A3="Result".
- A "Conversion formulas dictionary" tab contains two columns A:FormulaName and B:Formula. An example - in A2 there would be "Inch->Cm" and in B2 it would be "'=Conversion!$A$1*2.54
- In the cell Conversion!A3, is it possible to use the formulas from the "Formulas dictionary"???
Another example to simplify my question further:
- Cell A1: '=IF(B1=1;"True";"False") (Note the apostrophe before the formula which makes it a plain text string)
- Cell B1: 1
- Cell A2 ???Execute formula written in A1??? : True
Thank you!
In Excel, I would like to know if it is possible to execute a formula written in another cell as a simple string of characters within a cell.
This way, it would be possible to replace an endless "IF" formula (else IF, else IF, etc.) with a lookup in a matrix of formulas, depending on the case at hand.
For example - Let's assume a sheet that converts measurement units (Celsius to Fahrenheit, gallons to liters, etc.):
- A "Conversion" tab contains two editable cells: A1="Value to convert" and A2="Conversion to apply" and a non-editable cell A3="Result".
- A "Conversion formulas dictionary" tab contains two columns A:FormulaName and B:Formula. An example - in A2 there would be "Inch->Cm" and in B2 it would be "'=Conversion!$A$1*2.54
- In the cell Conversion!A3, is it possible to use the formulas from the "Formulas dictionary"???
Another example to simplify my question further:
- Cell A1: '=IF(B1=1;"True";"False") (Note the apostrophe before the formula which makes it a plain text string)
- Cell B1: 1
- Cell A2 ???Execute formula written in A1??? : True
Thank you!
Configuration: Windows XP Internet Explorer 6.0
9 réponses
Hello everyone,
It is indeed possible to do this without VBA but with a trick.
I'm sharing this as information (VBA offers more flexibility), but for a one-time need, it can be useful, especially if you're not comfortable with VBA.
In an older version of Excel (Excel 4?), there was the function =EVALUATE(A2) that allowed you to calculate the result of the function written as TEXT in A2. For example, A1: 5 A2: '=A1*2.54
The function (more documented) has been disabled. If I enter in A3: =EVALUATE(A2), I get 'invalid function'.
However, by defining a name, you can get around this.
Menu 'Insert / Name / Define...'
Name: function_variable
Refers to: =EVALUATE(Sheet1!$A$2)
In A3, I enter the name: =function_variable and I get the result of '=A1*2.54', which is 12.7
I haven't looked in detail to see if it's easily applicable to your case because you already have the VBA solution from gb, but it was an opportunity to remind you of this trick.
Eric
It is indeed possible to do this without VBA but with a trick.
I'm sharing this as information (VBA offers more flexibility), but for a one-time need, it can be useful, especially if you're not comfortable with VBA.
In an older version of Excel (Excel 4?), there was the function =EVALUATE(A2) that allowed you to calculate the result of the function written as TEXT in A2. For example, A1: 5 A2: '=A1*2.54
The function (more documented) has been disabled. If I enter in A3: =EVALUATE(A2), I get 'invalid function'.
However, by defining a name, you can get around this.
Menu 'Insert / Name / Define...'
Name: function_variable
Refers to: =EVALUATE(Sheet1!$A$2)
In A3, I enter the name: =function_variable and I get the result of '=A1*2.54', which is 12.7
I haven't looked in detail to see if it's easily applicable to your case because you already have the VBA solution from gb, but it was an opportunity to remind you of this trick.
Eric
Is it possible to display, not the address of the cell, but the value of it
instead of showing: A1*2.54, display (if A1=3): 3*2.54