Function max(ByRef a()) As Variant
Dim max1#
max1 = a(LBound(a))
For i = LBound(a) + 1 To UBound(a)
If a(i) > max1 Then
max1 = a(i)
End If
Next i
max = max1
End Function
Private Sub Form_Load()
Dim a#, b#, c#, d() As Variant, e#
a = InputBox("enter a number")
b = InputBox("enter a number")
If a < b Then
c = a
Else
c = b
End If
ReDim d(1 To c)
For i = 1 To c
If a Mod i = 0 And b Mod i = 0 Then
d(i) = i
Else
d(i) = 0
End If
Next i
e = max(d)
Print e
End Sub
如果改变数组定义成双精度就不行了提示“缺少数组或用户定义类型”,为什么呢?
Function max(ByRef a()) As double
Dim max1#
max1 = a(LBound(a))
For i = LBound(a) + 1 To UBound(a)
If a(i) > max1 Then
max1 = a(i)
End If
Next i
max = max1
End Function
Private Sub Form_Load()
Dim a#, b#, c#, d() As double, e#
a = InputBox("enter a number")
b = InputBox("enter a number")
If a < b Then
c = a
Else
c = b
End If
ReDim d(1 To c)
For i = 1 To c
If a Mod i = 0 And b Mod i = 0 Then
d(i) = i
Else
d(i) = 0
End If
Next i
e = max(d)
Print e
End Sub