Need help making a function that returns the equation of a cell without the equal sign. Can be done in google sheets, Excel, VBA, or Google App Scripts.
Example below
Instead of typing out A1+A2, can I type a formula into cell C1 that returns string A1+A2 from cell B2.
B2 Formula =A1+A2
C2 Value I would like A1+A2
example
Try this quick user defined function.
function whatFormula(rng as range)
set rng = rng.cells(1)
if rng.hasformula then _
whatFormula = mid(rng.formula, 2)
end function
Related
I'm an Excel VBA newbie.
How to change the value of the specified cell via a user-defined function? What's wrong with this code:
Function Test(ByVal ACell As Range) As String
ACell.Value = "This text is set by a function"
Test := "Result"
End Function
My wish is ... when I type =Test(E6) in cell E1, Excel will display the specified text in E6.
YES, of course, it is possible.
Put this code in Module1 of VBA editor:
Function UDF_RectangleArea(A As Integer, B As Integer)
Evaluate "FireYourMacro(" & Application.Caller.Offset(0, 1).Address(False, False) & "," & A & "," & B & ")"
UDF_RectangleArea = "Hello world"
End Function
Private Sub FireYourMacro(ResultCell As Range, A As Integer, B As Integer)
ResultCell = A * B
End Sub
The result of this example UDF is returned in another, adjacent cell. The user defined function UDF_RectangleArea calculates the rectangle area based on its two parameters A and B and returns result in a cell to the right. You can easily modify this example function.
The limitation Microsoft imposed on function is bypassed by the use of VBA Evaluate function. Evaluate simply fires VBA macro from within UDF. The reference to the cell is passed by Application.Caller. Have fun!
UDF limitation documentation: https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel
A VBA UDF can be used as an array function to return results to multiple adjacent cells.
Enter the formula into E1 and E2 and press Ctrl-Shift-Enter to create a multi-cell array formula. Your UDF would look something like this:
Public Function TestArray(rng As Range)
Dim Ansa(1 To 2, 1 To 1) As Variant
Ansa(1, 1) = "First answer"
Ansa(2, 1) = "Second answer"
TestArray = Ansa
End Function
Excel VBA will not allow a user-defined function to alter the value of another cell.
The only thing a UDF is allowed to do (with a few minor exceptions) is to return values to the cells it is called from.
Why not just typing you formula in E6 then ? That's the Excel logic: put your formula where you want the result to appear.
I am trying to extract values from preceding rows based on a value which is given in next columns.
Like in following example (Explanation Table Image), in cell A2 Cell value is 10 (Highlighted in Green) the corresponding value in cell D2 is 3 (Highlighted in Red) so the value required in Cell E2, E3 & E4 is values of cells A3, A4 and A5 which are A-1111, B-2222, C-3333 after removing text "SLR# " respectively. And in case of Cell A6 the corresponding value in cell D6 is 2 so the required value in cell E6 & E7 will be D-4444 & E-5555.
The data continuous like this and formula or VBA code will fill my requirement in column E. Kindly let me know if you need further clarification. Thanks in advance...
Explanation Table:
I have tried the following User Defined Function but it gives me all values in one cell rather in next cell. (Thanks to Mr. Fluff for this UDF)
Function UDF(Rng As Range, Rws As Long) As String
Dim i As Long
Dim Cl As Range
For Each Cl In Rng
If Left(Cl, 4) = "SLR#" Then
UDf = UDF & Cl.Value
i = i + 1
If i = Rws Then Exit For
End If
Next Cl
UDF = Trim(Replace(fiberboysa, "SLR#", ""))
End Function
I could be simplifying this but if the data is ALWAYS structured that way then try this formula ...
=IF(D3<>"","",IF(ISBLANK(A3),"",A3))
... copy that into cell E2 and fill down.
I'm happy if it doesn't do it for you but it worked with the simple example you gave.
I suppose this should be easy, but - how do I extract a value from each cell in a range, place that value in a formula, and return the result in the same cell that the original value came from?
Example: (simplified :-))
I already have values entered in a range (say A1:A3): A1=2.1 ; A2=0.78 ; A3=1.1. I also have a specific factor in D1 e.g. D1=0.4, and a specific formula that I want to use: exp(ln(value)/factor). What I want to do is:
extract the value in A1 (=2.1) and
place it in the formula together with the factor in D1 (=exp(ln(2.1)/0.4))
and put the result back in A1 (=6.39)
...and so on for A2 (=0.54), A3 (=1.27)......
As my range is very large, covering several sheets, I´m thinking some kind of "for each cell in range(myRange)" function, but I haven´t been able to figure it out...
To accomplish this, try the following:
Dim rng As Range, Cell As Range
Then set your Range:
Set rng = Range("A1:A3")
Then begin your For Each loop:
For Each Cell In rng
Cell = Exp(Log(Cell)/Range("D4"))
Next Cell
It Sum of columns is storing in one cell and if I want the same answer to be printed in another cell, what is the formula to get this?
Let's say your sum is stored in B1 and you want same in C1 then you can do
=B1
inside C1 cell
Let's say you want the value of cell A1 in cell B1. You would use this formula in B1:
= A1
In code you would write something like this (example in VBA):
Dim sht as Worksheet
Set sht = ThisWorkbook.Worksheets("MySheet") //Or whatever your target sheet is
sht.Range("B1").Formula = "=" & sht.Range("A1").Address
I need to do a Mid or Find on a cell formula rather than cell contents.
If my cell formula is:
=[Func](Arg1, Arg2, Arg3)
I need to be able to extract say Arg2 to another cell.
Is this possible without using VBA or a special Excel Add-In?
I've tried using the Cell but there is no option to return the formula a cell contains, if I could just break that formula out as a string to another cell I would be able to use Mid and Find etc.
Any Ideas?
Cheers
Yes, this is indeed one of those cases, where only a UDF (user defined formula) does the trick. Here a possible function (not too sophisticated); you could certainly add more of your required processing into this UDF.
Function FormulaText(cell As Range, _
Optional default_value As Variant)
' Returns the formula of the top leftmost cell in range as text
' default_value Value to return, if cell is empty
Set cell = cell.Resize(1, 1)
If cell.Formula = "" And Not IsMissing(default_value) Then
FormulaText = default_value
Else
FormulaText = cell.Formula
' If using a non-Englisch Version of Excel, cell.FormulaLocal
' returns the Formula in the localized format
' instead of e.g.
' =SUM(C7, C9) in my case it would return
' =SUMME(C7;C9)
End If
End Function
Open the Visual Basic Editor (Alt + F11), create a new module and paste the code into the edit area.
Use in Excel:
=FormulaText(A1, "No formula") with default value if referenced cell is empty
=FormulaText(A1) without default value
=FormulaText(A1:B3) return only the formula from A1
HTH
Andreas