VBA的5個內置數組函數

2021-12-08 14:30 更新

Array函數

Array函數允許你在代碼執(zhí)行中間創(chuàng)建一個數組,而不必事先確定其大小。該函數總是返回一個Varant數組。使用函數Array你可以快速地將一系列數據放置在一個清單里面。下面的過程CarInfo創(chuàng)建了一個叫做auto的固定大小,一維的三個成員的數組。
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
另外一個例子,示范如何使用Array函數將列標輸入到工作表里:
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 聲明一動態(tài)數組
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函數。該函數刪除靜態(tài)或動態(tài)數組儲存的所有數據,另外,對于動態(tài)數組,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
當你要確定一個二維數組的上下界時,你就必須明確維數: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) & "."


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號