2016-12-19 6 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

@Gary Evansの回答は仕事をするだろうし、理解しやすい。私はそのように行くだろう。とにかく、一般的な解決策(むしろあいまいではないが、私は恐れている)のために[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

TWOディメンションの配列を使用しないのはなぜですか? –

+0

@ D.O。うん、いい点だ....私は過去に、第二次元の長さが大幅に上下することができるので、このようにしなければならないことを知っています。しかし、私は多次元を考えないために馬鹿げています。 –

0

これに対する2次元の答えは以下のようになります。これを行うには多くの方法がありますが、これは単なる例です。 2次元配列は素晴らしいですが、実装の周りにいくつかの考えが必要です。理想的には、それを実装するために何らかの形の再帰を使用したいと思うでしょう。以下の例では静的な方法でそれらを設定しています。

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 

ヨーヨーはその宣言をしていない場合にはサイズ(だから変更することができます)を使用する前にそれをサイズする必要があります。それを行う、リセットする、または保存するには2つの方法があります。

これにより、配列がクリアされ、新しいサイズに設定されます。配列のサイズが以前の100で、データが入っていた場合は、以下のようにすべてのデータが削除されますが、データは大きくなります。

Redim AryTable(200) 

配列のサイズが以前に100だったデータにそれを持っていた場合、以下のすべてのデータを保持し、あなたが唯一の第二を調整するための2つの次元配列で、それは大きな

Redim Preserve AryTable(200) 

になるだろう寸法。以下okです: - 以下

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

失敗します - あなたは、テーブルのようなデータを格納するために2次元配列を使用したい場合は、これを考慮して

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

を最初のディメンションを使用します2番目の行は列のカウントがめったに変化しませんが、行が追加される可能性があります。

+0

こんにちはゲイリー。ありがとうございますが、問題は2次元配列を使用してコードを作成する方法ですIsError(Application.Match(Range( "... – awariat