2010-11-05 75 views
2

我需要对单元格公式执行Mid或Find,而不是单元格内容。从Excel公式提取参数或字符串 - Excel 2003

如果我的细胞公式为:

= [FUNC](ARG1,Arg2所得,ARG3)

我需要能够提取说Arg2所得到另一个小区。

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

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

任何想法?

干杯

回答

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