如何在VBA过程中使用可选的数组参数? [英] How can I use an optional array argument in a VBA procedure?

查看:429
本文介绍了如何在VBA过程中使用可选的数组参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBA脚本中有一个用于MS Access的私有过程:

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 = ????)

如您所见,我想为值数组提供一个可选的last参数.

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 s数组,而不是仅一个Variant s数组.不需要默认值:

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 Variants, as opposed to just array of Variants. 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.

For consistency, also declare as plain Variants the other two parameters.

如果您讨厌IDE,请不要使用它. 使用记事本.然后粘贴书面代码.

If you hate the IDE, do not use it. Use notepad. Then paste written code.

这篇关于如何在VBA过程中使用可选的数组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆