W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
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
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
' 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就顯示一個空信息框了。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) & "."
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: