excel - 定义返回空白的自定义 VBA 函数

标签 excel vba user-defined-functions

如果公式的结果为空白,如何定义一个返回空白的excel函数?

如果 vlookup 找到空白,则返回零。我想将 vlookup 嵌入一个函数中,如果 vlookup 找到一个空白单元格,则返回一个空白而不是零。

我不想输入:
=if(vlookup(args) = "", "", vlookup(args))
我希望有类似于内置 Iferror 函数的东西,其语法为:

=blankreturn(function)

如果函数结果为空白,则返回空白,否则返回函数的结果。

我试图定义一个自定义函数,但是当它找到一个空白单元格时它仍然返回零
public Function Blankreturn(formula as variant)

If IsEmpty(formula) = True Then
   Blankreturn = ""
Else
   Blankreturn = formula
End if

End Function

甚至更好,尽管我不知道从哪里开始编写代码:
ifblank(function, "This is blank")

如果第一个参数为空白,则返回第二个参数。

谢谢

最佳答案

你可以建立你的特殊 VLookup功能

Option Explicit

Public Function MyVLookup(Arg1, Arg2, Arg3, Optional Arg4 = True)
    MyVLookup = Application.VLookup(Arg1, Arg2, Arg3, Arg4)
    If VarType(MyVLookup) = 0 Then MyVLookup = vbNullString
End Function

enter image description here

但可能使用 VBA 用户定义函数比使用此方法要慢 =if(vlookup(args) = "", "", vlookup(args))我可以想象(需要测试)。

关于excel - 定义返回空白的自定义 VBA 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54694520/

相关文章:

vba - Excel VBA - 范围及其子范围的并集

c# - Excel VBA 项目没有关闭

打开文件时未启用 Excel ActiveX 列表框

excel - 如何查找后期绑定(bind)引用的名称

python - 将 pyspark 数据帧中的多列转换为一个字典

r - 在 R 中,如何选择和应用函数向量的元素?

excel - 将使用的范围复制到文本文件

excel - 范围类的自动过滤方法因条件失败

Range类的VBA排序方法失败

c++ - 二次公式 c++ - 用户定义的函数