VBAの5つの組み込み配列関数

配列関数(Array関数)

配列関数を使用すると、事前にサイズを決定しなくても、コード実行の途中で配列を作成できます。この関数は常にVarant配列を返します。 配列関数を使用すると、一連のデータをリストにすばやく配置できます。次のプロシージャCarInfoは、autoと呼ばれる固定サイズの1次元の3メンバー配列を作成します。

1、現在のプロジェクトに新しいモジュールを挿入し、名前をArray_Functionに変更します。

2、次のプロシージャCarInfoを入力します。

Option Base 1
Sub CarInfo()
Dim auto As Variant
auto = Array("Ford", "Black", "1999")
MsgBox auto(2) & " " & auto(1) & ", " & auto(3)
auto(2) = "4-door"
MsgBox auto(2) & " " & auto(1) & ", " & auto(3)
End Sub
別の例では、配列関数を使用して列ラベルをワークシートに入力する方法を示します。
Sub ColumnHeads()
Dim heading As Variant
Dim cell As Range
Dim i As Integer
i = 1
heading = Array("First Name", "Last Name", "Position", _
"Salary")
Workbooks.Add
For Each cell in Range("A1:D1")
cell.Formula = heading(i)
i = i+1
Next
Columns("A:D").Select
Selection.Columns.AutoFit
Range("A1").Select
End Sub

IsArray関数

Sub IsThisArray()
'declare a dynamic array 動的配列を宣言する
Dim sheetNames() As String
Dim totalSheets As Integer
Dim counter As Integer
'count the sheets in the current workbook 現在のワークブックにある
ワークシートの数を数える
totalSheets = ActiveWorkbook.Sheets.Count
'specify the size of the array 配列サイズを指定する
ReDim sheetNames(1 To totalSheets)
'enter and show the names of sheets ワークシート名を入力して表示する
For counter = 1 to totalSheets
sheetNames(counter) = ActiveWorkbook.Sheets(counter).Name
MsgBox sheetNames(counter)
Next counter
'check if this is indeed an array 配列であるかどうかを確認する
If IsArray(sheetNames) Then
MsgBox "The sheetNames is an array."
End If
End Sub

Erase関数

配列内のデータをクリアしたい場合は、Erase関数を使用する必要があります。この関数は、静的または動的配列に格納されているすべてのデータを削除します。さらに、動的配列の場合、Erase関数は、元々配列に割り当てられていたすべてのメモリを再割り当てします。次の例は、配列citiesのデータを削除する方法を示しています。

1、現在のプロジェクトに新しいモジュールを挿入し、名前をErase_Functionに変更します。

2、次のプロシージャFunCitiesを入力します。

' start indexing array elements at 1
Option Base 1
Sub FunCities()
'declare the array
Dim cities(1 to 5) As String
'assign the values to array elements
cities(1) = "Las Vegas"
cities(2) = "Orlando"
cities(3) = "Atlantic City"
cities(4) = "New York"
cities(5) = "San Francisco"
'display the list of cities
MsgBox cities(1) & Chr(13) & cities(2) & Chr(13) _
& cities(3) & Chr(13) & cities(4) & Chr(13) _
& cities (5)
Erase cities
'show all that was erased
MsgBox cities(1) & Chr(13) & cities(2) & Chr(13) _
& cities(3) & Chr(13) & cities(4) & Chr(13) _
& cities (5)
End Sub

Erase関数が配列内のデータをクリアした後、MsgBox関数は空白のメッセージボックスを表示します。

LBound関数とUBound関数

LBound関数とUBound関数は、それぞれ配列の下限と上限を示す数値を返します。

1、モジュールを現在のプロジェクトに挿入し、L_and_UBound_Functionという名前を付けます。

2、次のコードFunCities2を入力します。

Sub FunCities2()
'declare the array
Dim cities(1 to 5) As String
'assign the values to array elements
cities(1) = "Las Vegas"
cities(2) = "Orlando"
cities(3) = "Atlantic City"
cities(4) = "New York"
cities(5) = "San Francisco"
'display the list of cities
MsgBox cities(1) & Chr(13) & cities(2) & Chr(13) _
& cities(3) & Chr(13) & cities(4) & Chr(13) _
& cities (5)
'display the array bounds
MsgBox "The lower bound: " & LBound(cities) & Chr(13) _
& "The upper bound: " & UBound(cities)
End Sub

2次元配列の上限と下限を決定する場合は、次元の数を指定する必要があります。1は1次元を意味し、2は2次元を意味します。この記事の前半のExchangeプロシージャの後に、次のステートメントを追加して、2次元配列の上限と下限を決定します。(キーワードEnd Subの前に次のコードを追加します)

MsgBox "The lower bound (first dimension) is " _
& LBound(Ex, 1) & "."
MsgBox " The upper bound(first dimension) is " _
& UBound(Ex, 1) & "."
MsgBox "The lower bound (second dimension) is " _
& LBound(Ex, 2) & "."
MsgBox " The upper bound(second dimension) is " _
& UBound(Ex, 2) & "."
Share

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です