2016-12-19 113 views
1

我有代码:多维数组或集合

Dim products As Variant 
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row 
products = Array("MS-CHOPMAT-6", "MS-BOARDS-3", "MS-CHOP-LR") 

For x = LastRow To 1 Step -1 

order_quantity = Range("$E$" & x).Value 
item_price = Range("$F$" & x).Value 

' if value not found inside the array using the "MATCH" function 
If IsError(Application.Match(Range("$D$" & x).Value, products, 0)) Then 
Range("$H$" & x).Value = "ERROR - " & order_quantity 
Else ' successful "MATCH" inside the array 
Range("$H$" & x).Value = order_quantity * 3 
End If 

Next 

但不是有简单的数组,我需要多维数组或“集合”。如何改变这种代码的集合或多维数组

喜欢的工作:

products = Array(Array("MS-CHOPMAT-6", 11,"w"), Array("MS-BOARDS-3", 12, 4), Array("MS-CHOP-LR", 13, 5)) 
+0

@加里埃文斯答案会做的工作,很容易理解。我会那样。无论如何,对于一个通用的解决方案(虽然很模糊,恐怕)看看[this](http://stackoverflow.com/a/37777993/1726522)。我已经使用它,它被证明是强大的。 – CMArg

回答

0

这可能会帮助,在数组的数组是不严格可能的,你可以做什么什么,我已经在做过去是通过创建自己的分隔符来模仿数组中的数组,如下所示。

Public Sub Sample() 
Dim AryTable() As String 
Dim AryRow() As String 
Dim VntCell  As Variant 
Dim LngID  As Long 

'AryTable is root array 
ReDim AryTable(2) 

'Below is the population of the array, using #~# as a delimiter, whatever 
'you feel will not come up will be best 
'In the past to be safe I used #UnliklyDivider# as my delimiter, to make 
'sure it was never confused or come up in 
'real data 
AryTable(0) = "1#~#Field1#~#Field2#~#Field3" 
AryTable(1) = "1#~#Field1#~#Field2#~#Field3#~#Field4#~#Field5" 
AryTable(2) = "1#~#Field1#~#Field2#~#Field3#~##~#Field5" 

'This goes through each row in the array, using each one as an array in its 
'own right 
For LngID = 0 To UBound(AryTable, 1) 
    AryRow = Split(AryTable(LngID), "#~#") 
    For Each VntCell In AryRow 
     Debug.Print LngID & ": " & VntCell 
    Next 
Next 

End Sub 
+1

数组中的数组绝对有可能。只需使用一个变体数组。 –

+0

为什么不使用具有两个维度的数组? –

+0

@ D.O。是的,这是一个好点....我知道在过去,我必须这样做,因为第二维的长度可以大幅度缩放,但是我没有考虑多维度,所以我觉得很愚蠢。 –

0

对此的二维答案如下。有很多方法可以做到这一点,这只是一个例子。二维数组虽然很好,但需要对实现进行一些思考,理想情况下,您希望使用某种形式的递归来填充它,下面的示例简单地以静态方式设置它们。

Public Sub Sample() 
Dim AryTable() As String 
Dim LngRow  As Long 
Dim LngCol  As Long 

'Below is a two dimensional array, think of it as a 
'table with 3 rows and 5 columns (the base is zero 
'so it is not 2 rows and 4 columns as it may look) 
ReDim AryTable(2, 4) 

'We can then populate (or not) each 'cell' of the array 

'Row 1 
AryTable(0, 0) = "1" 
AryTable(0, 1) = "Field1" 
AryTable(0, 2) = "Field2" 
AryTable(0, 3) = "Field3" 

'Row 2 
AryTable(1, 0) = "2" 
AryTable(1, 1) = "Field1" 
AryTable(1, 2) = "Field2" 
AryTable(1, 3) = "Field3" 
AryTable(1, 4) = "Field4" 

'Row 3 
AryTable(2, 0) = "3" 
AryTable(2, 1) = "Field1" 
AryTable(2, 2) = "Field2" 
AryTable(2, 4) = "Field4" 

'Ubound by the first dimension to go through the rows 
For LngRow = 0 To UBound(AryTable, 1) 

    'Ubound by the second dimension to go through the columns 
    For LngCol = 0 To UBound(AryTable, 2) 
     Debug.Print AryTable(LngRow, 0) & ": " & AryTable(LngRow, LngCol) 
    Next 
Next 

End Sub 

需要注意的是,如果您在开始时未声明数组的大小,以后可以更改它。

该声明(以及以后不能更改): -

Dim AryTable(1,2) as string 

这不是声明(及以后可以更改): -

Dim AryTable() as string 

当哟尚未宣布大小(所以可以改变它),你必须在使用前调整它的大小。有两种方法可以做到,重置或保存。

这将清除数组并将其设置为新的大小,即,即。如果数组之前的大小为100,并且有数据存在,则下面的数据将删除所有数据,但将数据扩大。

Redim AryTable(200) 

如果阵列中尺寸以前100有数据它,下面将保留所有数据,并使其更大

Redim Preserve AryTable(200) 

在二维数组,你只能调整第二尺寸。以下是确定: -

Redim AryTable(2,4) 
Redim Preserve AryTable(2,8) 

以下将失败: -

Redim AryTable(2,4) 
Redim Preserve AryTable(4,8) 

考虑到这一点,如果你想用一个二维数组存储就像一个表中的数据,使用第一个维度为列,第二个为行,列计数很少变化,但可以添加行。

+0

嗨加里。非常感谢你,但问题是如何使用二维数组与我的代码如果IsError(Application.Match(Range(“... – awariat