简体   繁体   English

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

[英]Define a custom VBA function that returns blank

How do I define an excel function that returns a blank if the result of the formula is blank? 如果公式的结果为空白,如何定义一个返回空白的excel函数?

If vlookup finds a blank, it returns a zero. 如果vlookup发现空白,则返回零。 I'd like to embed vlookup in a function that, if vlookup finds a blank cell, returns a blank instead of a zero. 我想将vlookup嵌入到一个函数中,如果vlookup找到一个空白单元格,则返回一个空白而不是零。

I don't want to have to type out: 我不想输入:

=if(vlookup(args) = "", "", vlookup(args))

I would prefer to have something similar to the built in Iferror function whose syntax would be: 我希望有一些类似于内置的Iferror函数的语法:

=blankreturn(function)

Which would return a blank if the function results in a blank, or the result of the function if it does not. 如果函数导致空白,则返回空白;否则,返回函数的结果。

I have tried to define a custom function but it still returns a zero when it finds a blank cell 我试图定义一个自定义函数,但是当找到一个空白单元格时它仍然返回零

public Function Blankreturn(formula as variant)

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

End Function

Or even better, though I wouldn't know where to start with the code for this: 甚至更好,尽管我不知道从何处开始:

ifblank(function, "This is blank")

Which, if the first argument results in a blank, returns the second argument. 如果第一个参数为空,则返回第二个参数。

Thanks 谢谢

You could build your special VLookup function 您可以构建特殊的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

在此处输入图片说明

But possibly using a VBA user definde function is slower than using this method =if(vlookup(args) = "", "", vlookup(args)) I could imagine (would need to be tested). 但是可能使用VBA用户定义函数比使用此方法慢=if(vlookup(args) = "", "", vlookup(args))我可以想象(需要进行测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM