Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 阵列2d VBA可选元素_Arrays_Vba_Multidimensional Array - Fatal编程技术网

Arrays 阵列2d VBA可选元素

Arrays 阵列2d VBA可选元素,arrays,vba,multidimensional-array,Arrays,Vba,Multidimensional Array,如何在VBA中的二维数组中添加可选元素?工作示例: sub test Dim arr As Variant, ix as variant arr = Array( _ Array("01probe", 1, 2, True), _ Array("02datum", 3, 4) _ ) '... lots more ' true optional, nothing or just "false" as

如何在VBA中的二维数组中添加可选元素?工作示例:

sub test
   Dim arr As Variant, ix as variant
   arr = Array( _
   Array("01probe", 1, 2, True), _
   Array("02datum", 3, 4) _
  )
  '... lots more
  ' true optional, nothing or just "false" as alternative

  For Each ix In arr
   Debug.Print ix(3) '<---- index out of range
  Next ix
end sub

谢谢大家

您的阵列不是2D类型。这是一个数组的数组一个锯齿状数组

但我无法想象为什么需要一个可选参数

如果数组被声明为Variant,则它可以保留任何类型的变量,包括对象。也不必声明其尺寸。一切都可以被认为是可选的

请参见下一代码:

Sub testOptionalArray()
 Dim arr As Variant, dbInteger As Integer, strProbeNr As Long, rng As Range
 Dim rng2 As Range, dbDate As Date, dDate As Date
 
  dbInteger = 11: strProbeNr = 1000: dbDate = Date + 1: dDate = Now
  Set rng = Range("A1:A2"): Set rng2 = Range("B1:B2")
  arr = Array( _
        Array("01probe", dbInteger, strProbeNr, rng), _
        Array("02datum", dbDate, dDate, rng2))
        
  Debug.Print arr(0)(1), arr(0)(3).cells(1, 1).value
  Debug.Print arr(1)(1), arr(1)(UBound(arr(1))).cells.count, arr(1)(UBound(arr(1))).Address
End Sub
已编辑,在发布返回错误的代码后:

sub test
Dim arr As Variant, i as integer
arr = Array( _
Array("01probe", 1, 2, True), _
Array("02datum", 3, 4) _
)

'... lots more

 For i = LBound(arr) To UBound(arr)
    Debug.Print (arr(i)(UBound(arr(i))))
Next i
end sub
必须以下一种方式理解讨论中的代码。首先,总是适当地声明所有变量是一个好习惯

Sub testJaggedArray()
Dim arr As Variant, ix As Variant
   arr = Array( _
   Array("01probe", 1, 2, True), _
   Array("02datum", 3, 4))
  'The above array must be understood like an array of two arrays, 
  'first of them having 4 elements (ubound = 4, because it starts from 0)
  ' and a second one with only 3 elements.

  For Each ix In arr
   'When your code tries to print the fourth element of an array with only 
   'three elements, of course VBA return 'subscript out of range'.
   Debug.Print ix(3) '<---- subscript out of range
  Next ix
  'You can return an element of an array inside a jagged array in this way:
   Debug.Print arr(1)(ubound(arr(1)))' which means returning of the last element
                                     ' of the second array without returning an error
  'You may use an array of two arrays having different number of element, 
  'but you cannot expect to an array to return an INEXISTENT item.
  'There is no any connection between an hypothetical need of OPTIONAL element.
  'It is only a matter of EXISTING element...
End Sub

在FaneDuru的帮助下,我得到了一个解决方案,可以使用不同的内部数组大小循环一个数组,而不会出现错误:

sub test
Dim arr As Variant, i as integer
arr = Array( _
Array("01probe", 1, 2, True), _
Array("02datum", 3, 4) _
)

'... lots more

 For i = LBound(arr) To UBound(arr)
    Debug.Print (arr(i)(UBound(arr(i))))
Next i
end sub

数组被声明为variant。你没有明白我的意思,我想省略rng2。不填写可选参数的原因是懒惰。任何东西都可以是可选的:不,我在foreach中获取超出范围的索引。@Timo:从哪里获取超出范围的索引?尝试我的上述代码?@Timo:如果我建议您编辑您的问题并在收到上述错误的地方共享foreach代码,我会问得太多吗?@Timo:谢谢!但回到你的问题上来,你能澄清上述问题吗?我可以承认,不可能漏掉你的观点。这就是为什么,你能更好地定义它吗?如果你对此不感兴趣,我也不会有问题…@Timo:我不确定你是否理解你运行的代码的含义。。。这不是可选参数的问题。您正在处理一个锯齿状数组,代码尝试打印一个不存在的数组元素。我会编辑上面的答案,并尝试解释…为了澄清我没有否决投票,但我可以问一下你想要实现什么吗?我的意思是,为什么不创建一个自定义对象,您可以单独访问其属性,然后拥有该对象的数组?使用数组,它可能比对象更简单。目前,如果不使用,我用false填充内部第四个元素。