在 vba 中声明和初始化字符串数组
在任何编程语言的任何代码执行中,存储信息的需求有时是不可避免的。好消息是 vba 在存储数据时允许多种选择,其中之一就是数组。
在 vba 中,数组根据其长度和数据类型的灵活性进行分类。
此外,vba 中的数组也可以根据它们的维度进行分类。
声明并初始化动态字符串数组
下面的代码块将演示如何创建和初始化字符串数组。
	方法 1 - 声明为 variant 数据类型
	通过在变体数据类型中创建变量来声明动态数组。然后数组将由一个集合(array())初始化。
sub dynamicarraydemo()
dim stringarray as variant
stringarray = array("lion", "tiger", "cheetah", "monkey", "elephant", "zebra")
debug.print stringarray(3)
end sub
	dynamicarraydemo 输出:
monkey
	方法 2 - 声明为字符串,然后使用 split() 函数
	声明一个名为 stringarray 的字符串数组,而不显式声明边界。
sub dynamicarraydemo()
dim stringarray() as string
dim str as string
str = "lion,tiger,cheetah,monkey,elephant,zebra"
stringarray = split("lion,tiger,cheetah,monkey,elephant,zebra", ",")
debug.print stringarray(2)
end sub
	dynamicarraydemo 输出:
cheetah
声明并初始化静态字符串数组
下面的代码块演示了声明和初始化静态字符串数组的不同方法。
	方法 1 - 声明 lowerbound 和 upperbound:
通过显式声明其第一个和最后一个元素来声明一个静态字符串数组。
语法:
dim stringarray([lowerbound] to [upperbound]) as string
参数:
| [lowerbound] | 数组的第一个元素所引用的键整数。 | 
| [upperbound] | 引用数组最后一个元素的键整数。 | 
	下面的示例将声明一个名为 stringarray 的字符串数组,其中包含从元素 0 到 5 的六个元素。
sub staticarraydemo()
dim stringarray(0 to 5) as string
stringarray(0) = "lion"
stringarray(1) = "tiger"
stringarray(2) = "cheetah"
stringarray(3) = "monkey"
stringarray(4) = "elephant"
stringarray(5) = "zebra"
debug.print stringarray(4)
end sub
	staticarraydemo 输出:
elephant
	方法 2 - 显式更改下限
	用一个通用的 lower bound 值声明一个字符串数组。
option base 1
sub staticarraydemo()
dim stringarray(6) as string
stringarray(1) = "lion"
stringarray(2) = "tiger"
stringarray(3) = "cheetah"
stringarray(4) = "monkey"
stringarray(5) = "elephant"
stringarray(6) = "zebra"
debug.print stringarray(1)
end sub
	staticarraydemo 输出:
lion
方法 3 - 使用多维数组声明和初始化
在 vba 中,你可以声明最多 60 维的数组。
语法:
dim stingarray( [lowerbound1] to [upperbound1],[lowerbound2] to [upperbound2], . . .  ) as string
参数:
| [lowerbound1] | 关键整数是第一个数组维度上引用的第一个数组元素。 | 
| [upperbound1] | 关键整数是第一个数组维度上引用的最后一个数组元素。 | 
| [lowerbound2] | 关键整数是在第二个数组维度上引用的第一个数组元素。 | 
| [upperbound2] | 关键整数是第二个数组维度上引用的最后一个数组元素。 | 
在下面的示例中,声明了一个多维数组,其中第一个维度是 1 到 5;然后另一个是 1 到 5。
sub multistaticarraydemo()
dim stringarray(1 to 5, 1 to 5) as string
dim i, j as integer
for i = 1 to 5
    for j = 1 to 5
        stringarray(i, j) = "the value of (" & i & "," & j & ") is " & i * j
        debug.print stringarray(i, j)
    next j
next i
end sub
	multistaticarraydemo 输出:
the value of (1,1) is 1
the value of (1,2) is 2
the value of (1,3) is 3
the value of (1,4) is 4
the value of (1,5) is 5
the value of (2,1) is 2
the value of (2,2) is 4
the value of (2,3) is 6
the value of (2,4) is 8
the value of (2,5) is 10
the value of (3,1) is 3
the value of (3,2) is 6
the value of (3,3) is 9
the value of (3,4) is 12
the value of (3,5) is 15
the value of (4,1) is 4
the value of (4,2) is 8
the value of (4,3) is 12
the value of (4,4) is 16
the value of (4,5) is 20
the value of (5,1) is 5
the value of (5,2) is 10
the value of (5,3) is 15
the value of (5,4) is 20
the value of (5,5) is 25
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2023/03/19 浏览次数:627 分类:vba
- 
                                本教程演示了如何在 vba 中使用具有多个条件的自动过滤器。 
发布时间:2023/03/19 浏览次数:383 分类:vba
- 
                                本教程演示如何在 vba 中设置工作表。 
