开发者

Extract Argument or String from Excel Formula - Excel 2003

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 an开发者_如何学Pythonother 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜