确定实际传递给 VBA 函数的(可选)参数有多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17351020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:55:33  来源:igfitidea点击:

Determine how many (optional) arguments where actually passed to VBA function?

vbaexcel-vbaexcel

提问by Kurt Peek

I'd like to define a function in Visual Basic which computes income tax in a given bracket. The inputs should be the income, the marginal tax rate, the lower bracket boundary, and - optionally - the upper bracket boundary. (For the top bracket there is no upper boundary).

我想在 Visual Basic 中定义一个函数,用于计算给定括号中的所得税。输入应该是收入、边际税率、下限界限,以及 - 可选 - 上限界限。(对于顶部支架没有上限)。

Here's how I went about it. First, I define a "ramp" function as follows:

这就是我如何去做的。首先,我定义了一个“斜坡”函数,如下所示:

Public Function ramp(x)
    ramp = (x + Abs(x)) / 2
End Function

which is basically the same as IF(x<0,0,x). Then I define the function (in Dutch) for the tax as

这与 IF(x<0,0,x) 基本相同。然后我将税收的函数(荷兰语)定义为

Public Function schijfbelasting(inkomen, ondergrens, bovengrens, tarief)
    schijfbelasting = ramp(tarief * (inkomen - ondergrens)) - ramp(tarief * (inkomen - bovengrens))
End Function

Here "inkomen"=income, "ondergrens"=lower bracket boundary, "bovengrens"=upper bracket boundary, "tarief"=marginal tax rate, and the output "schijfbelasting"=tax in the specified bracket.

这里“inkomen”=收入,“ondergrens”=下括号边界,“bovengrens”=上括号边界,“tarief”=边际税率,并且输出“schijfbebirth”=指定括号中的税。

This all works fine, except that I'd like to make the "bovengrens" (upper bracket boundary) optional using

这一切都很好,除了我想使用“bovengrens”(大括号边界)可选

Optional bovengrens

In Matlab, I would use the "nargin" (number of arguments) function to do something like the following:

在 Matlab 中,我将使用“nargin”(参数数量)函数来执行以下操作:

Public Function schijfbelasting(inkomen, ondergrens, Optional bovengrens, tarief)
If nargin==4
    schijfbelasting = ramp(tarief * (inkomen - ondergrens)) - ramp(tarief * (inkomen - bovengrens))
Elseif nargin==3
schijfbelasting = ramp(tarief*(inkomen-ondergrens))
End If
End Function

However, I'm not aware of a function similar to "nargin" in Visual Basic. It could also be something like "if the argument "bovengrens" is defined". Does anybody know how to approach this problem? Thanks in advance.

但是,我不知道 Visual Basic 中类似于“nargin”的函数。它也可能类似于“如果定义了参数“bovengrens””。有人知道如何解决这个问题吗?提前致谢。

P.S. I am aware that I can make the code 'work' by filling in a very large number for the bracket 'boundary' in the top bracket, but I do not consider this elegant coding.

PS 我知道我可以通过为顶部括号中的括号“边界”填写一个非常大的数字来使代码“工作”,但我不考虑这种优雅的编码。

回答by Doug Glancy

You can use VBA's IsMissingfunction to test for optional parameters. Here's an example:

您可以使用 VBA 的IsMissing函数来测试可选参数。下面是一个例子:

Public Sub OptionalArg(arg1, Optional arg2)
Debug.Print arg1; IIf(IsMissing(arg2), "missing", arg2)
End Sub

Test it like this:

像这样测试它:

Sub Test()
OptionalArg 1
OptionalArg 1, 2
End Sub

回答by RBarryYoung

Just test the arguments using the IsMissingfunction:

只需使用IsMissing函数测试参数:

If IsMissing(bovengrens) Then ...