首页 > 解决方案 > 将函数参数作为公式传递

问题描述

这是我的 VBA 脚本的代码:

Function custom_if_formula(condition)
    MsgBox(condition)    
End Function

我将公式粘贴到任何单元格:

=custom_if_formula(B1="something")

MsgBox 中的结果是:TRUEFALSE。结果是否有可能进入 MsgBox B1="something"

我想要实现的伪代码:

Function custom_if_formula(condition)
    condition = condition.formula 'any method which take a literal string
    MsgBox(condition)    
End Function

PS 我的目标是实现我自己的 IFS 函数,其行为与 Excel 2016 中的行为相同。我只是好奇它是否可能。这就是为什么我不想将字符串作为参数传递。

标签: excelvba

解决方案


好的,我找到了一种方法:

Function custom_if_formula(condition)
    cell_formula = Range(Application.Caller.Address).Formula 'I get an adress of a cell which call an UDF, and then take all string it contains
    arg = Mid(cell_formula, 12, 100)
    MsgBox(arg)
End Function

推荐阅读