arrays - vba从数组数组中提取数组

标签 arrays excel string vba

我有许多数组,其中包含不同的字符串,例如

array1() = Array("one","two","three")
array2() = Array("siz","help")

我将字符串组合成字符串数组的数组
bigArray() = Array(array1(),array2(),...)

For k = 0 To # in bigArray
    reason = causes(xValue, bigArray(k))
Next

但是,当我尝试将 bigArray(k) 传递给函数时,出现错误
Public Function causes(xValue As String, keyWords() As Variant) As String

最佳答案

作业左侧的空括号是多余且令人困惑的,请删除它们。

我猜bigArray被声明为这样,如果有的话:

Dim bigArray [As Variant]

If it's not declared, specify Option Explicit at the top of your module (always do this!), and declare every variable you're using - otherwise you allow VBA to happily compile & run typos, and that inevitably turns into embarrassing, hard-to-find bugs, and possibly duplicate questions on Stack Overflow.



一个 Variant可以容纳任何东西,包括数组或锯齿状数组(即数组数组)。
keywords参数虽然...
Public Function causes(xValue As String, keyWords() As Variant) As String

被声明为一个数组,其中每个元素都是一个变体。虽然变体元素确实可以是一个数组,但是当您将数组作为参数传递时,没有办法说“每个元素都是变体元素的数组”,所以如果你只是包装它,你会更容易在 Variant (然后断言您正在查看一个数组):
Public Function causes(xValue As String, keyWords As Variant) As String
    Debug.Assert IsArray(keyWords)
    Debug.Assert IsArray(keyWords(LBound(keyWords))

您的 For循环假设下边界是:

 For k = 0 To # in bigArray


不做任何假设的循环是:
For k = LBound(bigArray) To UBound(bigArray)

关于arrays - vba从数组数组中提取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51312029/

相关文章:

arrays - 保持点相关性,同时展平二维网格

VBA 替换忽略列/表限制

regex - 基本正则表达式问题 : How to modify only the lines starting with a STRING with sed

c# - 如何创建包含真实图像的字节数组?

javascript - 将数组拆分为子数组并替换字符

python - 如何迭代或递归确定二维数组中的邻居?

string - 在 Scala 中使用 f 字符串插值器强制指定小数点分隔符

Excel:如果三个匹配,则从第三列返回值

vba - 范围(单元格(...工作不正确

java - 为什么另一部分没有打印在下一行?