mdsk.net
当前位置:首页 >> vBA 求数组最大值 >>

vBA 求数组最大值

假如数组为 arr 最大值为:Application.Max(arr)

Sub test()Dim arr(3) As Integerarr(0) = 1arr(1) = 3arr(2) = 4arr(3) = 2maxs = arr(0)For i = 0 To UBound(arr()) If arr(i) >= maxs Then maxs = arr(i)NextMsgBox maxsEnd Sub这样应该可以吧

dim a(10) a(0)=1 a(1)=2'省略 for i = 0 to 10 if maxnext msgbox max

你声明数组变量如下;Dim str5(1 To 5) As Integer不能用:Dim str5(5) As Integer如果用 Dim str5(5) As Integer,系统就会指用 Dim str5(0 to 5) As Integer

Sub dd() Dim arr arr = Array(1, 2, 3, 4, 5, 6) [A2] = Application.WorksheetFunction.Max(arr) End Sub 用工作表函数MAX就可以了.

求最大值为例 for i=0 to ubound(arr) for j=i+1 to ubound(arr) if arr(i)temp=arr(i) arr(i)=arr(j) arr(j)=temp end if enxt j next i msgbox arr(0)

代码如下:'arr()为你给定的目标数组,n为你指定的前n个最大值,调用例子:' Dim arr() As Variant' arr = Array(1, 4, 5, 6, 2, 6, 8, 4, 6, 7) ' PrintPreN arr, 10 Private Sub PrintPreN(arr() As Variant, n As Long) Dim Brr() As Variant '排序后的数组

'给你用过程写,其实应该用函数写的,试一下 Private Sub Command1_Click() Dim arr(1 To 10), i As Long, getmax For i = LBound(arr) To UBound(arr) Randomize arr(i) = Int(Rnd * 100) + 1 Next Print Join(arr, ",") ismax getmax, arr Print "最

如果有几个最大值咋办? dim iTest(n) as integer dim iplace(n) as integer dim max as integer dim i as integer dim j as integer for i= 0 to n-1 if iTest(i)>iTest(i+1) then max = iTest(i) else max = iTest(i+1) end if next for i= 0 to n if iTest(i)=max then iplace(j) = i end if j=j+1 next 最大值:max 位置:iplace()

Private Sub Command1_Click() Dim a(4, 4) As Integer'----------以下为对这个5行5列数组赋值并显示出来---------- For i = 0 To 4 For j = 0 To 4 a(i, j) = Int(Rnd * 90) + 10 Print a(i, j); Next Print Next'----------以上为对这个5行5列数组赋值并显示出来--------

网站首页 | 网站地图
All rights reserved Powered by www.mdsk.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com