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
Related
I have a dropdown in i7. In j7 I have a formula, that adjusts the hyperlink -- based on value in i7. HLinks are to different cells in the same worksheet.
Trying to get XL to automatically jump to j7 upon value change in i7, and to follow/execute the corresponding HLink, meaning for the j7 to act as if it was clicked on (but without the use of sendkeys-left mouse click).
So far either line of below code, executed one at a time - on j7, gives "Run-time error '9': Subscript out of range"
Sub HLink_follow()
ActiveCell.Hyperlinks(1).Follow
ActiveWorkbook.FollowHyperlink ActiveCell.Hyperlinks(1).Address
End Sub
Am aware that this all can be done via VBA, without even having j7, but want to keep it the way that it is.
If you are not clear on something, ask a question.
When you use the Hyperlink-function in a formula, it doesn't add an entry to the hyperlink-collection of a cell. With other words, ActiveCell.Hyperlinks.Count is 0 and ActiveCell.Hyperlinks(1) therefore gives an Subscript out of range.
There is a question here on SO about that, have a look to this answer: https://stackoverflow.com/a/40343924/7599798. It suggests to parse the formula (split it by quote character) to get the URL and use FollowHyperlink with the extracted URL.
Now your case is more complicated as your cell doesn't contain only a simple =Hyperlink(..)-formula and therefore parsing is not an option. I see 2 possible solutions:
a) Instead of using a formula, add the Hyperlink via the Link-menu in Excel or the Worksheet.Hyperlink.Add method. Then add the logic to change the address in the Change-Trigger of your worksheet whenever some relevant data is changed.
b) Use a helper cell to calculate the destination of the hyperlink. The helper cell (let's say H7) would get a formula like IF(COUNTIF(I7,"x1"),"#A591",IF(COUNTIF(I7,"x2"),"#A665")). Your HLink_follow-routine would use the calculated address of that helper cell while the formula in J7 would have the simple formula =Hyperlink(H7, "See pic")
P.S. Yes, I am located in Europe - just look at my profile
Private Sub Worksheet_Change(ByVal Target As Range)
'auto selects cell
Dim MyVariable As String
MyVariable = Range("k8").Value
Application.Goto Reference:=Range(MyVariable)
End Sub
K8 contains a substitute formula to strip "#" from K7.
Works as expected.
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.
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.
What formula do you use to check if another cell has formula? For example, I have 2 columns, A has cells which contains either a formula or a value.
(Column A usually contains Formulas but other users try to change their values by directly typing and replacing the formula that was previously there)
In Column B I want to add a formula that will say "HasFormula" if the cell on Column A has formula and say "PlainValue" if it contains a value.
I'm thinking maybe using =ISNUMBER() but that may not be accurate.
I am using Excel 2010.
Excel actually has a builtin ISFORMULA() function.
Say A1 has a formula and you want to check that. In say B1, you can use:
=If(ISFORMULA(A1),"HasFormula","PlainValue")
Edit: Per your comment, you don't have ISFORMULA(). An alternative is to create a quick UDF, and use the custom function in the worksheet.
In a workbook module, put this code:
Function isFormula(ByVal target As Range) As Boolean
isFormula = target.hasFormula
End Function
Then you can call it like this: =isFormula(A1) and it will return TRUE if A1 has a formula.
If you can't use VBA, then you can use this formula:
=IF(ISERROR(FORMULATEXT(A1)),"PlainText","HasFormula")
The MrExcel website (link below) has this method which uses old code from Excel 4 (which is still present for backward compatibility)...
Define a NAME such as "CellToLeftHasFormula" and in the "refers to" box put
=GET.CELL(48,OFFSET(INDIRECT("RC",FALSE),0,-1))
Then in column B use the formula =CellToLeftHasFormula which will return TRUE if it has.
Be aware that this will mean your Excel will now contain a macro and so will need to be saved as such (xlsm). I use this in Excel 2010.
For full explanation (and other .CELL options, besides 48) see MrExcel link: https://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
You can use the Range.HasFormula property.
https://learn.microsoft.com/en-us/office/vba/api/excel.range.hasformula
EDIT:
Text and code from the above link:
"True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise. Read-only Variant. ..."
Worksheets("Sheet1").Activate
Set rr = Application.InputBox( _
prompt:="Select a range on this worksheet", _
Type:=8)
If rr.HasFormula = True Then
MsgBox "Every cell in the selection contains a formula"
End If
You can restrict the user by protecting the column A.
You can directly check if a cell contains a formula by using a shortcut Ctrl + `.
You can use vba and write a user defined function :
1. Press alt + F11
2. Insert module in workbook
3. Paste this code
Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
4. Now, use Isformula in the cell wherever you want.
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.