EXCEL中瞬时排序脚本

3/30/2009来源:Excel教程人气:7188

Dim x As Integer  '这是在通用中声明的变量

PRivate Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim num(250), num1 As String
Dim k As Integer

For i = 2 To 51
For j = 1 To 5
num1 = Cells(i, j).Value
If num1 <> "" Then
    If Not IsNumeric(num1) Then
    MsgBox ("错误!请输入有效数字(可包括负数和小数及有效的科学记数该可)")
    Cells(i, j).Value = ""
    i = 55
    j = 6
    k = k - 1
    End If
   
   num(k + 1) = Cells(i, j).Value
   k = k + 1
End If
Next j
Next i

For i = 1 To k
For j = i + 1 To k
If CSng(num(i)) > CSng(num(j)) Then
num1 = num(i)
num(i) = num(j)
num(j) = num1
End If
Next j
Cells(2 + Int((i - 1) / 5), 7 + (i - 1) Mod 5).Value = num(i)
Next i

If x <> k Then
For i = k + 1 To x
Cells(2 + Int((i - 1) / 5), 7 + (i - 1) Mod 5).Value = ""
Next i
Cells(1, 7).Value = "排序后的数列如下(共有" & k & "个数)"
x = k
End If

 

End Sub