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
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 Have built a string using a formula in excel. as an example
Cell C3 contains text "Languages"
Cell C4 = "English, Spanish,German, French"
My Forumla = C3 & ":" & CHAR(10) & C4
The Desired text would be:
Languages:
English, Spanish, German, French
(where the bold text would actually be some color like red)
Is there a way to do this in Excel (change partial text formatting) .
I Have tried a formula... (Not working)
Function formatText(InText As Range)
'Set font color
InText.Characters(1.5).Font.Color = Red
'InText.Characters((InStr(1, ":", InText) + 1), (Len(InText) - InStr(1, ":", InText))).Font.ColorIndex = 3
End Function
Your posted function with work if and only if
It is called from a Sub (ie, as other have mentioned, not as a UDF)
And
the value(s) contained in range InText are string constants. (This is the main point of my answer)
It will not work for any cells in range InText containing a formula. AFAIK you cannot format part of a string returned by a formula.
BTW I would love to be proved wrong on this!
Regarding Hightower's question, "how would you cast a formula output to a string so that you can apply the text formatting?"
To "cast" the output of a formula so that you can apply text formatting, you must write the value returned by the formula into the spreadsheet, then apply the formatting to the value you wrote. You could either write the value into the cell containing the formula (which will erase the formula), or you could write the value into a different place in the spreadsheet (which will preserve the formula but then you'll be seeing double).
Sub Cell_Format(InText as Range)
InText.formula = cstr(InText.value) ' converts result of formula into a string literal
'or: InText.offset(0,1).formula = cstr(InText.value) -- writes the value in the cell next to InText
InText.characters(1, 5).font.color = vbRed
End Sub
Then Cell_Format range("$A$1") will replace the formula in cell $A$1 with a string constant and change the color of the first five characters to red.
If you want to do this for a range larger than one cell, add this code to the above:
Sub Range_Format(InText as Range)
For each c in InText
Cell_Format(c)
Next
End Sub
You need to use this code:
InText.Characters(1,5).Font.Color = RGB(255, 0, 0)
If you want to make it flexible, so that only the (fully) second line is red, use this code:
InText.Characters(Instr(InText, vbCr)+1, Len(InText)-Instr(InText, vbCr)).Font.Color = RGB(255, 0, 0)
Note that your function needs to be called from VBA, i.e. you cannot use it as a User-Defined-Function! UDFs can only return a result but never modify a cell!
You cannot directly call the below UDF in Excel interface. For that you will have use an event as UDF cannot change the physical characteristic of a cell. For more details you may read this link. http://support.microsoft.com/kb/170787
Function formatText(InText As Range)
'Set font color
InText.Interior.Color = vbRed
'InText.Characters((InStr(1, ":", InText) + 1), (Len(InText) - InStr(1, ":", InText))).Font.ColorIndex = 3
End Function
Look at the column A contain value like the below image . I want a formula to do that .
I don't want to solution like
=CONCATENATE(A1,",",A2,",",A3,",",A4,",",A5)
Excel 2016 has a new function called TextJoin() with the syntax
=TextJoin(delimiter, ignore empty cells, range)
With this, you can use the formula
=TEXTJOIN(",",TRUE,A1:A5)
Edit after comment: It's a new formula in Excel 2016. It does not exist in 2010.
But there are many User Defined Functions (UDF) macros that do a better job than Concatenate, and that can be used in Excel 2010. For example in this post by Jon Acampora. You will need to use the VBA code in each of the spreadsheets where you want to use that special function, though, and all these spreadsheets need to be macro-enabled sheets for these special functions to work.
try this
=A1&","&A2&","&A3&","&A4
OR create custom VBA function
Public Function Join(rng As Range, delimiter As String) As String
Dim cell As Range
For Each cell In rng
Join = Join & cell.Text & delimiter
Next cell
' remove the last delimiter
Join = Left(Join, Len(Join) - Len(delimiter))
End Function
For a long list a more complex formula but one that 'auto adjusts' on copy down might suit:
=IF(A2="",LEFT(B1&A2&",",LEN(B1&A2&",")-2),B1&A2&",")
Assumes a row is inserted at the top with B1 blank. The output would be in ColumnB of the row with the first blank cell in ColumnA after Row1.
How do I search a cell for specific text in its formula(not the cell output) using Excel 2010. For example…I need to know if the highlighted cell has a “/” in the formula.
In this case the answer would be "No" or "False"
I have tried a few different options that all seem to search the cell output for the character instead of the cell formula. Here is what I have tried that does not work:
=ISNUMBER(SEARCH("/",A1))
=SUBSTITUTE(A1,"/","") <> A1
If you have a recent version of Excell (> 2010) you can use the function FORMULATEXT like here:
=ISNUMBER(SEARCH("/",FORMULATEXT(A1)))
Otherwise you can replace that with a VBA function:
Function FormulaText(mycell As Range)
FormulaText = mycell.Formula
End Function
Is there a way of displaying the cell name of a particular cell in another cell? I would like to display the cell name in the adjacent cell so that the user is able to identify the cell name without clicking it.
ADDRESS(ROW(),COLUMN()) will give you the address, e.g. $A$1 of the current cell. Add/subtract from the row/column values (numbers) to reference the cell you are after.
If you don't want the $ then you can find and replace it with SUBSTITUTE(ADDRESS(ROW(),COLUMN()),"$","") and get just A1 for example
This function would give the name of the NamedRange the cell belongs to:
Public Function CellName(oCell As Range) As Variant
Dim oName As Name
For Each oName In ThisWorkbook.Names
If oName.RefersToRange.Parent Is oCell.Parent Then
If Not Intersect(oCell, oName.RefersToRange) Is Nothing Then
CellName = oName.Name
Exit Function
End If
End If
Next
CellName = CVErr(xlErrNA)
End Function
It loops through all the names in the workbook, and then for each name it checks if it refers to any thing in this the sheet input parameter is from. If it is then it checks if the input cell and the rages referred by the name intersect. If they do it returns the name of the range.
In Excel 2013, and maybe in some older versions too, ADDRESS() accepts third parameter that defines the format of the address to be returned with following values:
1 - Absolute (default)
2 - Absolute row/Relative column
3 - Relative row/Absolute column
4 - Relative
so lets say in cell A1
ADDRESS(ROW();COLUMN()) //outputs $A$1
ADDRESS(ROW();COLUMN();1) //outputs $A$1
ADDRESS(ROW();COLUMN();2) //outputs A$1
ADDRESS(ROW();COLUMN();3) //outputs $A1
ADDRESS(ROW();COLUMN();4) //outputs A1
If you want to display the name of cell D3 in cell A1 type:
ADDERSS(ROW(D3);COLUMN(D3);4) //outputs text D3 in cell A1
Technically, you could combine the SUBSTITUTE() and new FORMULATEXT() functions to do this, if you don't mind adding a hidden column in your sheet.
Assume cell A1 is named FOO
Add a simple reference formula in B1 to the named cell =FOO
Add a formula in C1 =SUBSTITUTE(FORMULATEXT(B2),"=","")
Hide Column B
Cell C1 will contain the value FOO (or the RC reference for unnamed cells)
Adjust as needed, your mileage (or kilometerage) may vary.
It doesn't seem to be possible, which is weird. You'd think that the cell() function should provide a way to get the name, but it doesn't. Bummer.
ActiveWorkbook.Sheets.Item(1).Cells(row, col).Name.Name
I took some 'advice' from the answer above from Adarsha. I got a similar result to the code below, with that loop and a few refinements. However my 'tip for excel' macros is to make your debugger your best friend.
Function name_of(clls)
'
name_of = ""
'
Dim nam As String
Dim rg As Range
Set rg = clls
'
nam = rg.Name.Name
'
name_of = nam
'
End Function 'name_of
A little bit of patience and perseverance, gave me exactly what I was looking for -- A user defined function to give me the defined name of a cell. What happens if the same Cell has more than one name? Try that and see. It is a simple test, so I want to leave that for you to experiment, learn and pass on your new knowledge.
This will work in very basic circumstances:
Public Function CellName(cel As Range) As Variant
Dim nm As Name
For Each nm In Names
If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Then
CellName = nm.Name
Exit Function
End If
Next
CellName = CVErr(xlErrNA)
End Function
It won't work if the cell is part of a named range, it won't show multiple names for the cell, it won't work for cells included in named formulae (like =OFFSET() ranges, for example).
The
"=" & cel.Parent.Name & "!" & cel.Address
thing is pretty clunky, too. There may be a better way to do the check. Creating a Range object from the RefersTo and using Intersect() might work.
Excel does have a function "Cell()" that you can get certain properties from.
You can use =Cell("row", K9) and get back row number 9 and there's an equivalent "col" parameter but it returns the column number (11) rather than the letter.
Reference the named cell in another cell, E12 in this case, and then use this formula: ="'"&FORMULATEXT(E12). This puts an apostrophe in front so it will show the name or formula as text.
You can place your cursor in an empty cell, type = then click on the named cell. It will display the cell contents. Then you change the format of the cell to text and it will show =