Convert Literal Text to Formula - excel

Using MS Excel 2010: I used the CONCATENATE formula to create a text string that looks like a formula and need a formula that will convert the text string to a formula; without the use of MS Excel Paste Special function or VBA/Macro.
Example: In Cell B2:G2 contains text, in Cell B4 I have CONCATENATE text formula that returns a text string
=TRIM(CONCATENATE(B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2))
I want Excel to interpret this string as a formula and return/show the value in Cell B6 as: "Always be yourself … do not go out and look for a successful personality and duplicate it.” - Bruce Lee
I have attempted using the INDIRECT formula and without success, not certain if it's my formula or if it's possible. SAMPLE: Cell B6:
=INDIRECT(CONCATENATE("B4"))

1) CONCATENATE and & are the same thing. In other words the formula you wrote as a string could be translated as:
=TRIM(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(B2)," "),C2)," "),D2)," "),E2)," "),F2)," "),G2)," "),H2))
If you are going to write out CONCATENATE for you function, then separate all he strings you want joined together as with a coma as has been pointed out by #MarcoVos in the comments to your question:
=CONCATENATE(B2," ",C2," ",D2," ",E2," ",F2," ",G2," ",H2)
Or if you want to use the more common form of & your formula would look like what you originally posted without the CONCATENATE out from like this:
=B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2
Those last two formulas will produce the same results.
2) The INDIRECT function will convert text to a cell references or address. It will not convert a formula as text and spit out the results of the formula. Macros Vos in his answer is correct in that FORMULATEXT() will display the formula in the referenced cell as text or a string. If you need to follow a sequence where you provide the string and then must convert the string into a formula, then I suggest you use the EVALUATE Function. You cannot use EVALUATE from a regular excel formula though. You can call it from a named range though. Create a named range. Lets for example call it EvalText. In the formula portion for creating the named range, enter:
=EVALUATE($B$4)
It will automatically add the sheet name. Now in any cell on your sheet you can enter:
=EvalText
and it will return whatever the string in B4 works out to be as a formula. It will spit out an error if its not a proper excel formula.

Why not do it the other way around?
Put:
=TRIM(CONCATENATE(B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2))
in cell B6 and
=FORMULATEXT(B6)
in cell B4.

The problem with the named "Range" is that it is not part of the Recalc-Loop.
Defining (in VBA) a function
Function MyEval(text As String)
MyEval = Evaluate(text)
End Function
and adding
Private Sub Worksheet_Change(ByVal Target As Range)
Application.CalculateFull
End Sub
to the codesheet of the worksheet under consideration
solves the problem.
Of course, the workbook than has t be saved as XLSM.

Related

How do I CONCAT a static formula with a changing value based on a cells contents in VBA or an Excel formula?

My formula is as follows:
=(CONCAT("=RSLINX|PLC1!",B3)
B3 = VarName1
The goal is to use the RSLINX function with the contents of a changing cell (B3)
I have also tried putting "=RSLINX|PLC1!" in it's own cell to reference as a string, as well as the following formula:
=INDIRECT(CONCAT("=RSLINX|PLC1",B3))
The objective is that B3 can change to a different cell (B4, B5, etc.) so that I can evaluate many different tags quickly, as the only way I've had the RSLINX function work is by manually typing in a correct tag name.
Resolved. I had to use the original formula:
=(CONCAT("=RSLINX|PLC1!",B3)
For all desired tags, then copy it and use "Paste Value" and Search & Replace all "RSLINX" with "=RSLINX" in the new columns.

Excel - sum cell values divided by "plus"

I have a cell (A1 which have the text "3+2". On the next cell I need to put a formula which is "=A1+1".
How can I tell excel that it has to do the sum on A1, so he can sum another values?
Try to use EVALUATE function. It is best way to do it without VBA, however needs simple trick:
While being in cell B1 add to name manager range "Result" with formula
=EVALUATE($A1)
Than just place in B1 formula
=Result
For more comprehensive description regarding EVALUATE function please refer to this link. Additionaly please notice that EVALUATE function can have different name in your local language.
Btw. similar topic was discussed here
If A1 contains something like
number+number
then in B1 enter:
=LEFT(A1,FIND("+",A1)-1)+MID(A1,FIND("+",A1)+1,999)+1
If A1 contains an arbitrary numeric expression as text, you would use a VBA UDF.
EDIT#1:
If you have a list of values separated by the + sign, then you can use this array formula:
=SUM((TRIM(MID(SUBSTITUTE(A1,"+",REPT(" ",255)),1+(ROW(A1:A999)-1)*255,255)) & "0")/10)+1
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar:
Assuming you have your x+xxx+xxxxxxxxxxxx value in cell A1, consider the following formula below:
=SUM(0+TRIM(MID(SUBSTITUTE("+"&A1,"+",REPT(" ",99)),ROW(INDEX(A:A,1):INDEX(A:A,(1+LEN(A1)-LEN(SUBSTITUTE(A1,"+","")))))*99,99)))
When you return the formula, be sure to press CONTROL+SHIFT+ENTER. You can then just drag it down for your other additions.
You need to create a VB Macro:
Open the Tools>Macro>Visual Basic Editor
Right click to Create a new Module (important!)
Double click on the just created new module and paste this code
Public Function EvaluateString(strTextString As String)
Application.Volatile
EvaluateString = Evaluate(strTextString)
End Function
Close the VB editor window
Now you can use =EvaluateString(A1) to get the corresponding value of A1.

Calling the same cell of different worksheets in EXCEL

I have a drop down list (with name of sheets) and based on that value, let's say that I select the Sheet4 as in the image, I want to bring to another sheet the value of that selection, let's say on the cell B8.
I know that this works:
=IF(B1="Sheet1", Sheet1!B8, IF(B1="Sheet2", Sheet2!B8, Sheet3!B8))
That's for just 3 sheets but is there a nicer or more efficient way to do this?
This is in general how all the sheets look like:
Use INDIRECT to construct a valid worksheet and cell reference from text-that-looks-like-a-worksheet-and-cell-reference 1
The indirect takes a string and turns it into a valid reference.
=INDIRECT("'" & B1 & "'!B8")
So in the case above it would create a string "'Sheet4!B8". Then the Indirect will turn it into a valid cell reference.
As Jeeped also pointed out in the comments The B8 reference since it is literal text it will not change if the formula is copied or dragged to another cell.
The B1 which is a cell reference and is relative will change as it is copied or dragged to different cells.
1 as per #Jeeped comment

Converting a Text String that Links Two Sheets into a working formula

I am trying to convert a text string into a formula, but my text string is a bit unusual because it describes a link between two worksheets. Here is the starting formula:
+TEXT("+'X:\Deals\ONE-PAGERS\_One-pagers (vM)\["&TRIM(LEFT(SUBSTITUTE($B9," ",REPT(" ",30)),64))&" vM.xlsx]Metrics'!$D$8",1)
I created this formula to avoid having to manually link a large number of spreadsheets. It captures the first two words of a cell, and places that into a text string that would establish a link, if it were a formula. This way, I can simply enter a company name into cell B9, and this formula will produce a text string that describes the link I want. Here is the formula output:
+'X:\Deals\ONE-PAGERS\_One-pagers (vM)\[xxx vM.xlsx]Metrics'!$D$8
As you can see, if this were a formula, it would generate a link it would return the value in Cell D8 of the xxx worksheet. I have tried the evaluate workaround (seen below) as well as the indirect function and neither seems to be working. Let me know if this doesn't make sense and thanks so much in advance for your help!
Function EvalCell(RefCell As String)
Application.Volatile
EvalCell = Evaluate(RefCell)
End Function
Evaluate and INDIRECT would both require the source workbook to be open, but you could use ExecuteExcel4Macro:
Function EvalCell(RefCell As String)
Application.Volatile True
application.ExecuteExcel4Macro(application.convertformula(mid$(RefCell, 2), xlA1, xlr1c1))
End Function
I'd avoid using too many of these functions though!

Excel formula to show linked cell ID

In an excel cell, I've placed a simple formula
=C4
The cell typically displays the value of cell C4, but instead I want to see the linked cell ID instead, which in this case is "C4".
Is there a formula to show me this? like:
=SHOWCELL(C4)
The reason I need this instead of simply typing the value of "C4" into the cell, is so Excel will maintain the link to the correct cell even if rows are inserted/deleted, AND show me which cell is linked.
You should be able to use the Cell function.
In Excel, the Cell function can be used to retrieve information about a cell. This can include contents, formatting, size, etc.
=Cell("address", C4)
This displays $C$4.
When inserting a row before C4, it is changed to $C$5.
In case you do not want the $ signs, one way would be the Substitute function:
=Substitute( Cell("address", C4), "$", "" )
You can create your own User Defined Function to achieve this. I call it "CellReference".
Usage:
=CellReference(B6)
displays "B6"
To use it, launch VBA, insert a module, and then copy the below into the module:
Function CellReference(cell As range) As String
CellReference = cell.Address(0, 0, xlA1)
End Function

Resources