VBA 二次元配列 宣言 初期化

通常の宣言方法

Dim client(3, 1) As String
client(0, 0) = “佐藤 一郎”
client(0, 1) = “東京都”
client(1, 0) = “山田 花子”
client(1, 0) = “札幌市”
client(2, 0) = “加藤 健一”
client(2, 0) = “名古屋市”
client(3, 0) = “中村 吾郎”
client(3, 0) = “東京都”

http://www.officepro.jp/excelvba/array/index5.html
二次元配列の初期化方法
http://homepage1.nifty.com/rucio/main/dotnet/shokyu/standard27.htm

Dim Numbers(,) As Integer = {{10, 2, 2}, {5, 6, 0}}

ただし、2002以降らしい

' ary2(0,0) = Nothing, ary2(0,1) = Nothing, ary2(0,2) = Nothing,
' ary2(1,0) = Nothing, ary2(1,1) = Nothing, ary2(1,2) = Nothing

のようなものを

Dim ary2(,) As String = New String(1, 2) {}

とも表現できる
http://park5.wakwak.com/~weblab/refInitArray.html

Dim StrArray(1 To 1, 1 To 2) As String

宣言もOK
http://www.excel-vba.net/excel-array-003.html
他にも

' arry2(0) = Nothing, arry2(1) = Nothing
Dim arry2()() As Integer = New Integer(1)() {}
' arry3(0)(0) = 1, arry3(0)(1) = 2, arry3(0)(2) = 3,
' arry3(1)(0) = 7, arry3(1)(1) = 8
Dim arry3()() As Integer = New Integer()() {New Integer() {1, 2, 3}, New Integer() {7, 8}}

のような表現もできる。
あとは

Dim arry5(1)() As Integer ' arry5(0) = Nothing, arry5(1) = Nothing
' arry5(0)(0) = 10, arry5(0)(1) = 20, arry5(0)(2) = 30
' arry5(1)(0) = 55, arry5(1)(1) = 66
arry5(0) = New Integer() {10, 20, 30}
arry5(1) = New Integer() {55, 66}

ぐるぐるループを回すときは
Dim StrArray(1, 2) As String

StrArray(0, 0) = “A0”
StrArray(0, 1) = “B0”
StrArray(0, 2) = “C0”
StrArray(1, 0) = “A1”
StrArray(1, 1) = “B1”
StrArray(1, 2) = “C1”
Dim i As Integer, j As Integer
Dim StrData As String
For i = 0 To 1
  For j = 0 To 2
    StrData = StrData & StrArray(i, j) & vbCrLf
  Next j
Next i
MsgBox StrData

http://www.excel-vba.net/excel-array-003.html
Excelのセルの中を一気に二次元配列に入れられるらしい

Dim values As Variant
values = Range(“A1:B10”)

アクセス方法(A4の値)

values(1, 4)

これは超便利。CSVを二次元配列にしたい場合に使える。
http://kazemati.blogspot.com/2008/09/vba.html

タイトルとURLをコピーしました