WEB开发网
开发学院软件教学办公软件Excel EXCEL中瞬时排序脚本 阅读

EXCEL中瞬时排序脚本

 2009-03-30 14:31:39 来源:WEB开发网   
核心提示:Dim x As Integer '这是在通用中声明的变量PRivate Sub Worksheet_SelectionChange(ByVal Target As Range)Dim num(250), num1 As StringDim k As IntegerFor i = 2 To 51For j =

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

Tags:EXCEL 瞬时 排序

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接