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上,似乎我需要明确列出我想要传递的值。但是,如果有可能传递不同数量的值,那么我不知道有多少我想要传递给函数?是否有一些使用具有可变维度的数组(一维数组)的方法?
2 个解决方案
#1
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对象(作为普通参数而不是ParamArray)
#2
You can pass a variable number of values to a function if the function includes a Param Array:
如果函数包含Param数组,则可以将可变数量的值传递给函数:
http://msdn.microsoft.com/en-us/library/538f81ec(v=VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa164809(v=office.10).aspx
#1
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对象(作为普通参数而不是ParamArray)
#2
You can pass a variable number of values to a function if the function includes a Param Array:
如果函数包含Param数组,则可以将可变数量的值传递给函数:
http://msdn.microsoft.com/en-us/library/538f81ec(v=VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa164809(v=office.10).aspx