vba - 在 UDF 中处理可选参数的最佳方法是什么?
问题描述
我有一个类似的功能
Public Function func(ByRef mandatory_arg1 As Range, ByRef mandatory_arg2 As Range, _
Optional ByRef optional_Arg1 As Range, Optional ByRef optional_arg2 As Range, _
Optional ByRef optional_arg3 As Range, Optional ByRef optional_arg4 As Range, _
Optional ByRef optional_arg5 As Range, Optional ByRef optional_arg6 As Range) As Double
func = WorksheetFunction.SumIfs(mandatory_arg1, ...) / WorksheetFunction.SumIfs(mandatory_arg2, ...)
End Function
处理缺少参数的情况的最佳方法是什么?正在使用类似于的 If-Else 结构
if IsMissing(optional_Arg1) or IsMissing(optional_Arg2) Then
' ...
EndIf
唯一的办法?或者会WorksheetFunction.SumIfs(...)
忽略Nothing
?
解决方案
使用 ParamArray 并循环遍历传递的范围。
Public Function func(ByRef mandatory_arg1 As Range, ByRef mandatory_arg2 As Range, _
ParamArray rngs()) As Double
dim i as long
If IsMissing(rngs) Then
'no optional ranges
End If
For i = LBound(rngs) To UBound(rngs)
'process rngs(i)
Next i
end function
推荐阅读
- vue.js - 在 v-col vuetify 中动态更改 cols
- python - pip ImportError:无法导入名称 NullHandler
- javascript - 旋转老虎机在第一次旋转后是不可预测的
- kendo-ui - 如何在剑道网格中获取选定行的索引
- android - 如何在Constraint Layout中实现如下动态宽度的布局?
- java - Java 8 结果链接
- java - 如何校准 bno055 陀螺仪传感器?
- python - 使用 cx_freeze 后缺少文件,exe 在 Anaconda 提示符下运行,但不在 cmd 中
- c# - 在 Arcus 库中,单个 IP 地址如何创建子网对象?
- azure-devops - azdo 管道步骤 - 如何在进一步的任务中使用 CreateStack 云形成模板输出