从Excel公式中提取参数或字符串 - Excel 2003

时间:2010-11-05 11:06:05

标签: excel

我需要对细胞配方而不是细胞内容进行Mid或Find。

如果我的细胞配方是:

= [Func](Arg1,Arg2,Arg3)

我需要能够将Arg2提取到另一个单元格。

这可以不使用VBA或特殊的Excel加载项吗?

我尝试过使用Cell但没有选项可以返回单元格包含的公式,如果我可以将该公式作为字符串分解为另一个单元格,我可以使用Mid和Find等。

任何想法?

干杯

1 个答案:

答案 0 :(得分:2)

是的,这确实是其中一种情况,只有UDF(用户定义的公式)可以解决问题。这里有一个可能的功能(不太复杂);您当然可以在此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

打开Visual Basic编辑器(Alt + F11),创建一个新模块并将代码粘贴到编辑区域。

在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 安德烈亚斯