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 声明一动态数组
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) & "."
MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式;它是三种 ASP.NET 编程模式其中之...
Ruby 提供了其他现代语言中很常见的条件结构。在这里,我们将解释所有的条件语句和 Ruby 中可用的修饰符。 if...else 语句语法if...
范围(Range)无处不在:January 到 December、 0 到 9、等等。Ruby 支持范围,并允许我们以不同的方式使用范围:作为序列的范围...
什么是 SOAP? 简单对象访问协议(SOAP,全写为Simple Object Access Protocol)是交换数据的一种协议规范。SOAP 是一种简单的基于 ...
介绍提供倒计时管理能力。代码演示基本用法span总时间:{{ current.total }}/spanspan剩余天数:{{ current.days }}/spanspan剩...