vba ParamArray ArgList() 中可变数量的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949263/
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
Variable number of arguments in ParamArray ArgList()
提问by Fionnuala
If I want to pass a number of values for the ParamArray arglist via an array, how do I do it? From what I've read so far, on VBA, it appears that I need to explicitly list the values that I want to pass. But what if there are potentially different numbers of values to pass, so I do not know in advance how many I'll want to pass to the function? Is there not some way of using an array (a one-dimensional array) with a variable dimension?
如果我想通过数组为 ParamArray arglist 传递多个值,我该怎么做?从我到目前为止所读到的内容来看,在 VBA 上,我似乎需要明确列出我想要传递的值。但是,如果要传递的值数量可能不同,因此我事先不知道要传递给函数的数量,该怎么办?没有某种方法可以使用具有可变维度的数组(一维数组)吗?
回答by barrowc
There are several ways to achieve that:
有几种方法可以实现:
- dimension the array to be larger than you require, keep a count of how many items actually get added and then
Redim Preserve
back to the correct size - use a
Collection
object instead (as a normal parameter rather than aParamArray
)
- 将数组的尺寸标注为大于您需要的尺寸,计算实际添加的项目数量,然后
Redim Preserve
恢复到正确的大小 - 改用
Collection
对象(作为普通参数而不是 aParamArray
)
回答by Fionnuala
You can pass a variable number of values to a function if the function includes a Param Array:
如果函数包含参数数组,您可以将可变数量的值传递给函数:
http://msdn.microsoft.com/en-us/library/538f81ec(v=VS.71).aspx
http://msdn.microsoft.com/en-us/library/538f81ec(v=VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa164809(v=office.10).aspx
http://msdn.microsoft.com/en-us/library/aa164809(v=office.10).aspx