瀏覽量:5次
①升序排序的VBA數(shù)組
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數(shù)組的降序排序
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
③針對中文字符的數(shù)組排序
如果你想針對字符數(shù)組進行排序,可參考如下的代碼
Sub Start() Dim arr() As Variant arr = Array("大", "眾", "計", "算", "機", "學(xué)", "習(xí)", "網(wǎng)")
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
[聲明]本網(wǎng)轉(zhuǎn)載網(wǎng)絡(luò)媒體稿件是為了傳播更多的信息,此類稿件不代表本網(wǎng)觀點,本網(wǎng)不承擔此類稿件侵權(quán)行為的連帶責任。故此,如果您發(fā)現(xiàn)本網(wǎng)站的內(nèi)容侵犯了您的版權(quán),請您的相關(guān)內(nèi)容發(fā)至此郵箱【779898168@qq.com】,我們在確認后,會立即刪除,保證您的版權(quán)。
官網(wǎng)優(yōu)化
整站優(yōu)化
渠道代理
400-655-5776