陣列學習系列1-VBA的5個內建陣列函式(5)
阿新 • • 發佈:2021-07-21
Array函式
Array函式允許你在程式碼執行中間建立一個數組,而不必事先確定其大小。該函式總是返回一個Varant陣列。使用函式Array你可以快速地將一系列資料放置在一個清單裡面。下面的過程CarInfo建立了一個叫做auto的固定大小,一維的三個成員的陣列。 1. 在當前工程裡插入一新模組,重新命名為Array_Function 2. 輸入下列過程CarInfo:另外一個例子,示範如何使用Array函式將列標輸入到工作表裡: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_Function2. 輸入如下過程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
當你要確定一個二維陣列的上下界時,你就必須明確維數:1表示第一維,2表示第二維。在本章早先時候將的Exchange過程裡的後面加上如下語句,可以確定該二維陣列的上下界(將下列程式碼加入到關鍵字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) & "."