按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
通常,当你改变一个数组的大小时,你将失去该数组原来的所有数据。语句ReDim将数组重新初始
化。
然而,你可以将新成员加入到现存的数组里去,通过在语句ReDim后面带上关键字Preserve。换句
话说,关键字Preserve保证重新改变大小的数组不会弄丢现有的数据。如果你忽略它,新数组将会
是空的。
第二个For…Next循环给数组myArray的第六,第七,第八,第九和第十个成员赋值。这次,数组成
员的数值是相乘的:counter * counter。VB使用粗体将数组其它的数值输入到合适的工作表的单
元格里面。
技巧7…7 确定数组大小
在使用数组之前,必须在Dim或ReDim语句里确定数组的大小。这意味着你不可以给数组成员赋值,
直到你使用Dim或者ReDim语句声明了该数组。
8。数组函数
你可以通过五个VBA内置函数来操作数组:Array; IsArray; Erase; LBound和UBound。接下来的章
129
… 页面 146…
节将示范每个函数在VBA过程里的使用。
9。Array 函数
Array函数允许你在代码执行中间创建一个数组,而不必事先确定其大小。该函数总是返回一个
Varant数组。使用函数Array你可以快速地将一系列数据放置在一个清单里面。下面的过程CarInfo
创建了一个叫做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
Selectionlumns。AutoFit
Range(〃A1〃)。Select
End Sub
10。IsArray 函数
使用IsArray函数你可以测试某个变量是否数组。如果该变量是个数组,那么IsArray函数返回True,
否则返回False。请看例子:
1。 在当前工程里插入模块,命名为IsArray_Function
2。 输入如下过程IsThisArray:
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。Sheetsunt
'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
130
… 页面 147…
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
11。Erase 函数
当你要清除数组里的数据时,应该使用Erase函数。该函数删除静态或动态数组储存的所有数据,
另外,对于动态数组,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就显示一个空信息框了。
12。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
131
… 页面 148…
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) & 〃。〃
13。数组中的错误
使用数组时,出错是很容易的。如果你试图给数组赋予比声明数组时更多的成员的话,VBA就会显
示错误信息“下标越界”
图7…4 该错误出现于试图访问并不存在的数组成员
假设你声明了一个包含6个成员的一维数组,而你却试图给第八个成员赋值,当你运行该过程时,
VB无法找到第八个成员,所以显示错误信息。点击调试按钮,VB将导致错误的代码行(见图7…5)
加亮。检查数组的声明语句,并且更改被加亮代码行括号里的索引号。
“下标越界”错误经常是由使用循环的过程引发的。下面的过程Zoo1就是这种情况的一个例子。在
用户取消在输入框里输入数据之前,循环里的语句反复被执行。在执行该过程时,当变量 i 等于4
的时候,VB无法在这个只有三个成员的数组里找到第四个成员,那么错误信息就出现了。修改后的
过程Zoo2示范了前面章节里介绍的LBound和UBound函数如何能够避免试图访问不存在的数组成员
的错误。
132
… 页面 149…
图7…5 当你点击错误信息的调试按钮,VB就会加亮引发错误的语句
1。 在当前工程里插入新模块,命名为Errors_In_Arrays
2。 输入下列过程Zoo1和Zoo2:
Sub Zoo1()
'this procedure triggers an error 〃Subscript out of range〃 本过程引发“下标越界”错
误
Dim zoo(3) As String
Dim i As Integer
Dim response As String
i = 0
Do
i = i +1
response = InputBox(〃Enter a name of animal:〃)
zoo(i) = response
Loop until response = 〃〃
End Sub
Sub Zoo2()
'this procedure avoids the error 〃Subscript out of range〃本过程避免“下标越界”错误
Dim zoo(3) As String
Dim i As Integer
Dim response As String
i = 1
Do While i》=LBound(zoo) And i