VBA的5個(gè)內(nèi)置數(shù)組函數(shù)

2021-12-08 14:30 更新

Array函數(shù)

Array函數(shù)允許你在代碼執(zhí)行中間創(chuàng)建一個(gè)數(shù)組,而不必事先確定其大小。該函數(shù)總是返回一個(gè)Varant數(shù)組。使用函數(shù)Array你可以快速地將一系列數(shù)據(jù)放置在一個(gè)清單里面。下面的過(guò)程CarInfo創(chuàng)建了一個(gè)叫做auto的固定大小,一維的三個(gè)成員的數(shù)組。
1.  在當(dāng)前工程里插入一新模塊,重命名為Array_Function
2.  輸入下列過(guò)程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
另外一個(gè)例子,示范如何使用Array函數(shù)將列標(biāo)輸入到工作表里:
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函數(shù)

Sub IsThisArray()
'declare a dynamic array 聲明一動(dòng)態(tài)數(shù)組
Dim sheetNames() As String
Dim totalSheets As Integer
Dim counter As Integer
'count the sheets in the current workbook 計(jì)數(shù)當(dāng)前工作簿里的工作表數(shù)目
totalSheets = ActiveWorkbook.Sheets.Count
'specify the size of the array 明確數(shù)組大小
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 檢查它是否確實(shí)為數(shù)組
If IsArray(sheetNames) Then
MsgBox "The sheetNames is an array."
End If
End Sub

Erase函數(shù)

當(dāng)你要清除數(shù)組里的數(shù)據(jù)時(shí),應(yīng)該使用Erase函數(shù)。該函數(shù)刪除靜態(tài)或動(dòng)態(tài)數(shù)組儲(chǔ)存的所有數(shù)據(jù),另外,對(duì)于動(dòng)態(tài)數(shù)組,Erase函數(shù)將重新分配原來(lái)分配給該數(shù)組的所有內(nèi)存。下面的例子教你如何刪除數(shù)組cities里的數(shù)據(jù)。

1.  在當(dāng)前工程里插入一新模塊,重命名為Erase_Function
2.  輸入如下過(guò)程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
在函數(shù)Erase清除數(shù)組里的數(shù)據(jù)后,函數(shù)MsgBox就顯示一個(gè)空信息框了。

LBound函數(shù)和UBound函數(shù)

LBound函數(shù)和UBound函數(shù)分別返回表明數(shù)組的下界和上界的數(shù)字。

1.  在當(dāng)前工程里插入模塊,命名為L(zhǎng)_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
當(dāng)你要確定一個(gè)二維數(shù)組的上下界時(shí),你就必須明確維數(shù):1表示第一維,2表示第二維。在本章早先時(shí)候?qū)⒌腅xchange過(guò)程里的后面加上如下語(yǔ)句,可以確定該二維數(shù)組的上下界(將下列代碼加入到關(guān)鍵字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) & "."


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)