函数重载和UDF在Excel VBA [英] Function Overloading and UDF in Excel VBA
问题描述
由于VBA似乎不支持这一点,任何人都可以建议一个很好的,非混乱的方式来实现同样的目标?我应该使用可选参数还是有更好的方法?
将参数声明为可选变体
,然后您可以使用 IsMissing()
或使用 TypeName()检查他们的类型,
,如以下示例所示:
公共功能Foo(可选v As Variant)作为变式
如果IsMissing(v)然后
Foo =Missing argument
ElseIf TypeName(v)=String然后
Foo = v& 加一个
Else
Foo = v + 1
如果
结束函数
可以从工作表中调用 = FOO(), = FOO(号码)或 = FOO( string )。
I'm using Excel VBA to a write a UDF. I would like to overload my own UDF with a couple of different versions so that different arguments will call different functions.
As VBA doesn't seem to support this, could anyone suggest a good, non-messy way of achieving the same goal? Should I be using Optional arguments or is there a better way?
Declare your arguments as Optional Variants
, then you can test to see if they're missing using IsMissing()
or check their type using TypeName()
, as shown in the following example:
Public Function Foo(Optional v As Variant) As Variant
If IsMissing(v) Then
Foo = "Missing argument"
ElseIf TypeName(v) = "String" Then
Foo = v & " plus one"
Else
Foo = v + 1
End If
End Function
This can be called from a worksheet as =FOO(), =FOO(number), or =FOO("string").
这篇关于函数重载和UDF在Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!