vba如何将数组带入自定义的函数
发布网友
发布时间:2023-08-04 17:20
我来回答
共3个回答
热心网友
时间:2024-11-15 00:14
窗体放一个Label和一个Command,代码如下:
Private Sub Command1_Click()
Dim i, a(19), max, min, sum, a1, a2, a3, a4, a5 As Integer
Label1.Caption = ""
Randomize
Label1.Caption = "20名学生成绩:" & vbCrLf
For i = 0 To 19
a(i) = Int(Rnd * 101)
Label1.Caption = Label1.Caption & a(i) & IIf(i = 19, vbCrLf, ",") & IIf(i = 9, vbCrLf, "")
If i = 0 Then
max = a(i)
min = a(i)
Else
If a(i) < min Then min = a(i)
If a(i) > max Then max = a(i)
End If
sum = sum + a(i)
Select Case a(i)
Case 0 To 59
a1 = a1 + 1
Case 60 To 69
a2 = a2 + 1
Case 70 To 79
a3 = a3 + 1
Case 80 To 89
a4 = a4 + 1
Case 90 To 100
a5 = a5 + 1
End Select
Next i
Label1.Caption = Label1.Caption & "最高分=" & max & vbCrLf
Label1.Caption = Label1.Caption & "最低分=" & min & vbCrLf
Label1.Caption = Label1.Caption & "平均分=" & Format(sum / 20, "0.00") & vbCrLf
Label1.Caption = Label1.Caption & "0 - 59分人数:" & a1 & vbCrLf
Label1.Caption = Label1.Caption & "60 - 69分人数:" & a2 & vbCrLf
Label1.Caption = Label1.Caption & "70 - 79分人数:" & a3 & vbCrLf
Label1.Caption = Label1.Caption & "80 - 89分人数:" & a4 & vbCrLf
Label1.Caption = Label1.Caption & "90 - 100分人数:" & a5 & vbCrLf
Call paixu(a)
Label1.Caption = Label1.Caption & "排序后20名学生成绩:" & vbCrLf
For i = 0 To 19
Label1.Caption = Label1.Caption & a(i) & IIf(i = 19, vbCrLf, ",") & IIf(i = 9, vbCrLf, "")
Next
End Sub
Private Sub Form_Load()
Label1.Move 0, 0, Me.Width
Label1.AutoSize = True
Label1.Caption = ""
End Sub
Private Sub paixu(b() As Variant)
Dim i As Integer, x As Integer
For i = LBound(b) To UBound(b)
For j = LBound(b) To i
If b(j) < b(i) Then x = b(i): b(i) = b(j): b(j) = x
Next
Next
End Sub
热心网友
时间:2024-11-15 00:15
VB中,数组本来就是以传址方式进行传递调用的。也就是说,你这个参数S,可以是一个数组,调用的时候,以一个数组调用,结果会传递返回给调用处。
热心网友
时间:2024-11-15 00:15
Private Sub Form_Load()
Dim s(), s1()
s = Array(2, 3, 4, 5, 6)
Dim t, L, K
t = 1
L = 2
K = 3
s1 = Test(s, t, L, K)
For i = 0 To UBound(s1)
MsgBox s1(i)
Next
End Sub
Function Test(s(), t, L, K)
Dim a
ReDim a(UBound(s))
For i = 0 To UBound(s)
a(i) = s(i) + t + L + K '
Next
Test = a
End Function追问你好!感谢啦 再问一个问题,如何通过vba实现函数积分和微分啊?积分下限是负无穷上限是实数,微分比如函数m对s求偏导