``````Private Sub numSort()
'产生20个[0,100]之间的整型随机数
Dim n(1 To 20) As Integer
Dim i As Integer
For i = 1 To 20
Randomize
n(i) = Rnd() * 101
Next
'打印排序前随机数
Dim outString As String
outString = "排序前随机数："
For i = 1 To 20
outString = outString & Str(n(i)) & "  "
Next
Debug.Print outString
'排序
Dim temp As Integer
Dim j As Integer
For i = 1 To 20
For j = i To 20
If (n(i) > n(j)) Then
temp = n(i)
n(i) = n(j)
n(j) = temp
End If
Next
Next
'打印排序后随机数
outString = "排序后随机数："
For i = 1 To 20
outString = outString & Str(n(i)) & "  "
Next
Debug.Print outString
End Sub``````

``````Private Sub numSort()
'产生20个[0,100]之间的整型随机数
Dim n(1 To 20) As Integer
Dim i As Integer
For i = 1 To 20
Randomize
n(i) = Rnd() * 101
Next
'打印排序前随机数
printArr("排序前的随机数：",n)
'排序
Dim temp As Integer
Dim j As Integer
For i = 1 To 20
For j = i To 20
If (n(i) > n(j)) Then
temp = n(i)
n(i) = n(j)
n(j) = temp
End If
Next
Next
'打印排序后随机数
End Sub
Private Sub printArr(s As String, k() As Integer)
Dim outString As String
outString = s
Dim i As Integer
For i = LBound(k) To UBound(k)
outString = outString & Str(k(i)) & "  "
Next
Debug.Print outString
End Sub``````

Call printArr("排序前的随机数：",n)

1. 作为一名教书匠，传授给学生的知识时，自己一定要谨慎验证，谨慎、谨慎、再谨慎，验证、验证、再验证，不然容易误人子弟…
2. 经验主义不可取，一定谨记
另：为了代码更方便移植、更通用，上面代码可优化成（使用符号常量与使用lbound与ubound两个系统函数）
``````
Option Compare Database
Private Const ARR_LOW = 1   '数组上标
Private Const ARR_UPP = 20  '数组下标``````

Private Sub numSort()
Dim n(ARR_LOW To ARR_UPP) As Integer
Dim i As Integer
For i = LBound(n) To UBound(n)
Randomize
n(i) = Rnd() * 101
Next
'打印排序前随机数
Call printArr("排序前的随机数：", n)

``````'排序
Dim temp As Integer
Dim j As Integer
For i = LBound(n) To UBound(n)
For j = i To UBound(n)
If (n(i) > n(j)) Then
temp = n(i)
n(i) = n(j)
n(j) = temp
End If
Next
Next
'打印排序后随机数
Call printArr("排序后的随机数：", n)``````

End Sub