国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 辦公 > Excel > 正文

VBA數組的升序、降序

2024-08-23 19:53:45
字體:
來源:轉載
供稿:網友

  ①升序排序的VBA數組

  Function UP(x()) As Variant()

  Dim i As Integer, j As Integer, a, d()

  ReDim sx(LBound(x) To UBound(x)), d(LBound(x) To UBound(x))

  d = x

  If LBound(x) = UBound(x) Then

  sx = d

  Exit Function

  End If

  For i = LBound(x) To UBound(x) - 1

  For j = i + 1 To UBound(x)

  If d(j) < d(i) Then

  a = d(j): d(j) = d(i): d(i) = a

  End If

  Next

  Next

  sx = d

  End Function

  ②VBA數組的降序排序

  Function Down(x()) As Variant()

  Dim i As Integer, j As Integer, a, d()

  ReDim sx(LBound(x) To UBound(x)), d(LBound(x) To UBound(x))

  d = x

  If LBound(x) = UBound(x) Then

  sx = d

  Exit Function

  End If

  For i = LBound(x) To UBound(x) - 1

  For j = i + 1 To UBound(x)

  If d(j) > d(i) Then

  a = d(j): d(j) = d(i): d(i) = a

  End If

  Next

  Next

  sx = d

  End Function

  ③針對中文字符的數組排序

  如果你想針對字符數組進行排序,可參考如下的代碼

  Sub Start() Dim arr() As Variant arr = Array("大", "眾", "計", "算", "機", "學", "習", "網")

  QuickSort2 arr(), 0, UBound(arr) Dim s As String For I = 1 To UBound(arr) s = s & arr(I) & " | " Next MsgBox s

  End Sub Sub QuickSort2(MyArray() As Variant, L, R) Dim tp tp = 1

  Dim I, J, X, Y I = L J = R X = MyArray((L + R) / 2) While (I <= J) While (StrComp(MyArray(I), X, tp) < 0 And I < R) I = I + 1 Wend While (StrComp(X, MyArray(J), tp) < 0 And J > L) J = J - 1 Wend If (I <= J) Then Y = MyArray(I) MyArray(I) = MyArray(J) MyArray(J) = Y I = I + 1 J = J - 1 End If gIterations = gIterations + 1 Wend

  If (L < J) Then Call QuickSort2(MyArray(), L, J) If (I < R) Then Call QuickSort2(MyArray(), I, R)

  End Sub


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 重庆市| 长丰县| 上虞市| 太湖县| 攀枝花市| 枞阳县| 丰顺县| 高淳县| 方山县| 栾城县| 钦州市| 九龙城区| 五华县| 衢州市| 自治县| 苗栗县| 广元市| 全南县| 新丰县| 上杭县| 武邑县| 大姚县| 韩城市| 抚顺市| 家居| 东莞市| 大同市| 礼泉县| 措勤县| 沾化县| 台北市| 霍林郭勒市| 镶黄旗| 新竹市| 东乡族自治县| 西乌珠穆沁旗| 南昌市| 柳州市| 松原市| 济南市| 阿合奇县|