vba写一个二维数组排序代码
时间: 2023-02-27 17:05:46 浏览: 201
VB二维数组排序源码
4星 · 用户满意度95%
可以使用下面的代码实现二维数组的排序:
Sub Sort2DArray()
Dim arr(1 To 3, 1 To 3) As Integer
Dim i As Integer, j As Integer
Dim temp As Integer
'初始化二维数组
For i = 1 To 3
For j = 1 To 3
arr(i, j) = Int((9 - 1 + 1) * Rnd + 1)
Next j
Next i
'二维数组排序
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
For m = i To UBound(arr, 1)
For n = j To UBound(arr, 2)
If arr(i, j) > arr(m, n) Then
temp = arr(i, j)
arr(i, j) = arr(m, n)
arr(m, n) = temp
End If
Next n
Next m
Next j
Next i
'输出排序后的二维数组
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
Debug.Print arr(i, j)
Next j
Next i
End Sub
该代码使用双重循环实现了二维数组的排序,并在排序完成后输出结果。
阅读全文