- meira
-
最简单的vba函数来解决这个问题:利用每个元素与数组内的所有元素相减,计算差大于0的个数,个数越多的数组越大
Private Sub CommandButton1_Click()
Dim arr, brr, num ‘arr未排序前的数组,brr排序后的数组
ReDim brr(1 To 10, 1 To 1)
arr = [{2, 1, 3, 4, 6, 7, 5, 0, 8, 9}]
For i = 1 To UBound(arr)
num = 0
For j = 1 To UBound(arr)
If arr(i) - arr(j) > 0 Then
num = num + 1
End If
Next j
brr(num + 1, 1) = arr(i) "brr数组就是按照从小到大的顺序排列好的
Next i
MsgBox brr(1, 1) & brr(2, 1) & brr(3, 1) & brr(4, 1) & brr(5, 1) & brr(6, 1) & brr(7, 1) & brr(8, 1) & brr(9, 1) & brr(10, 1)
End Sub
- LuckySXyd
-
Private Sub Command1_Click()
Dim N As Integer, Ar() As Integer, i As Integer, j As Integer, T As Integer
N = Val(InputBox("输入数字的个数")) - 1
ReDim Ar(N)
For i = 0 To N
Ar(i) = Val(InputBox("输入第" & i & "个数字:"))
Next i
Print "排序前的结果是:"
For i = 0 To N
Print Ar(i); " ";
Next i
For i = N To 1 Step -1
For j = 0 To i - 1
If Ar(j) < Ar(j + 1) Then
T = Ar(j)
Ar(j) = Ar(j + 1)
Ar(j + 1) = T
End If
Next j
Next i
Print
Print "排序后的结果是:"
For i = 0 To N
Print Ar(i); " ";
Next i
End Sub
- 再也不做稀饭了
-
示例,数组a(4)排序:
dim a(4),ETemp,i,aindex,j,bign
a(0)=10
a(1)=6
a(2)=10
a(3)=29
a(4)=1
for j = lbound(a) to ubound(a)
for i = lbound(a) to ubound(a)
if a(i)<>"" Then
if bign = "" Then
bign = a(i)
end if
if a(i)>=bign Then
bign = a(i)
aindex = i
end if
end if
next
a(aindex) = ""
ETemp = ETemp & bign & ","
bign=""
next
ETemp = left(ETemp,len(ETemp)-1)
msgbox ETemp
如果你想将排序后的数字放进数组的话加一个变量:
dim b
b = split(ETemp,",")
b数组就是排序后的。
如果在vb中,b变量貌似应声明为:
dim b()
- 小菜G
-
最简单的排序,须借助excel的函数:
Sub SmallSort()
Dim a(-1 To 3), i, b()
For i = LBound(a) To UBound(a)
a(i) = Int(Rnd * 10) "赋值给a()
Next
Rows("5:6").Delete
[a5].Resize(1, UBound(a) - LBound(a) + 1) = a
"下面是排序方法,结果放在b()内
ReDim b(1 To UBound(a) - LBound(a) + 1)
For i = 1 To UBound(b)
b(i) = Application.WorksheetFunction.Small(a, i) "Small为从小到大,large为从大到小
Next
[a6].Resize(1, UBound(b)) = b
End Sub