1 函数解释: 将一个字符串按照某个子字符串分割成字符数组。
Function Split(Expression As String, [Delimiter], [Limit As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare])
Member of VBAStrings
Split a string into an array
2 例子
Private Sub Command1_Click()
Dim x As String
x = "a b c"
Dim y
y = Split(x, " ")
For i = 0 To UBound(y)
DebugPrint y(i)
Next
End Sub
vb中没有这个函数,你可以自己写一个嘛。
public
function
max(a
as
single,
b
as
single)
as
single
if
a
>
b
then
max
=
a
else
max
=
b
end
function
这样你就可以使用它了,如
debugprint
max(125,16)
这时就会输出125与16中较大的一个数。
当然,你也可以把以上自定义函数中的两个变量,换成一个数组,这样就可以不只是在两个变量之间返回最大值,而是在一个数组中返回最大值。
如:
public
function
max(a()
as
single)
as
single
dim
i
as
integer,
p
as
single
p
=
a(1)
for
i
=
2
to
ubound(a)
if
p
<
a(i)
then
p
=
a(i)
next
i
max
=
p
end
function
这样你可以先定义一个数组,并把你要查找最大值的数据存入这个数组中,再调用这个函数查找。
如:
dim
dat()
as
single
dat(1)=
dat(2)=
debugprint
max(dat)
这样就可以输出一组数组中最大值。
Private Sub Command2_Click()
Dim i As Integer
Dim s As String
Randomize
For i = 0 To 9
a(i) = Rnd 100 + 100
s = s & Str(a(i))
Next i
Label1Caption = s
End Sub
Private Function GetMax() As Integer
Dim i As Integer
Dim imax As Integer
For i = 0 To 9
If a(i) > imax Then imax = a(i)
Next i
GetMax = imax
End Function
Private Sub Command3_Click()
Label2Caption = Str(GetMax)
End Sub
给你一个排序的代码 只是最后显示的是最大值
Private Sub Command2_Click()
x = InputBox("输入第一个数")
y = InputBox("输入第二个数")
z = InputBox("输入第三个数")
'按从小到大排序
If x > y Then
a = x
x = y
y = a
End If 'x<y
If y > z Then
a = y
y = z
z = a 'y<z
End If
If x > y Then
a = x
x = y
y = a
End If 'x<y,此处y为原来的z
Print Tab(5); "最大值为:"; z
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)