如何在 VBA 过程中使用可选的数组参数? [英] How can I use an optional array argument in a VBA procedure?
问题描述
我在 MS Access 的 VBA 脚本中有一个私有过程:
I've got a private procedure in a VBA script for MS Access:
Private Sub drawLineDiagram(chartSpace As Variant, title As String, caption As String, x_val() As Variant, y_val() As Variant, Optional y_val2() As Variant = ????)
如您所见,我想为一组值设置一个可选的最后一个参数.
As you see, I want to have an optional last parameter for an array of values.
我必须分配什么样的默认参数?如果我用一个可选的整数值来做它并分配它,例如0 没事.
What kind of default parameter must I assign? If I do it with an optional integer value and assign it e.g. 0 it's all fine.
如果我使用如上所示的数组执行此操作并分配一个数组,则该行标记为红色 => 作为错误(并且不会编译).
If I do it with an array as shown above and assign an array, the line is marked red => as an error (and it won't compile).
推荐答案
如果您在 VBA 中需要一个可选数组,请将其声明为 Variant
而不使用数组说明符,但无论如何都可以将其作为数组访问.通过这种方式,您将获得一个 Variant
(单个变量),其中包含一个 Variant
数组,而不仅仅是 Variant
数组.不需要默认值:
If you need an optional array in VBA, declare it as Variant
without array specificator, but access it as an array anyway. This way you get a Variant
(single variable) that holds an array of Variant
s, as opposed to just array of Variant
s. No default value is required:
Private Sub drawLineDiagram(chartSpace As Variant, title As String, caption As String, x_val As Variant, y_val As Variant, Optional y_val2 As Variant)
为了一致性,另外两个参数也声明为普通的Variant
s.
For consistency, also declare as plain Variant
s the other two parameters.
如果您讨厌 IDE,请不要使用它.使用记事本.然后粘贴写好的代码.
If you hate the IDE, do not use it. Use notepad. Then paste written code.
这篇关于如何在 VBA 过程中使用可选的数组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!