VBA数组的升序、降序

A话别离

A话别离

2016-02-19 14:09

图老师设计创意栏目是一个分享最好最实用的教程的社区,我们拥有最用心的各种教程,今天就给大家分享VBA数组的升序、降序的教程,热爱PS的朋友们快点看过来吧!

①升序排序的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

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/diannaorumen/)

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

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/diannaorumen/)

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

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/diannaorumen/)

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

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/diannaorumen/)

Next

Next

sx = d

End Function

③针对中文字符的数组排序

如果你想针对字符数组进行排序,可参考如下的代码

Sub Start() Dim arr() As Variant arr = Array("大", "众", "计", "算", "机", "学", "习", "网")

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/diannaorumen/)

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

展开更多 50%)
分享

猜你喜欢

VBA数组的升序、降序

电脑入门
VBA数组的升序、降序

动态数组

Delphi
动态数组

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

使用数组

编程语言 网络编程
使用数组

Excel vba返回的路径

电脑入门
Excel vba返回的路径

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

mysql降序索引和减轻索引扫描

编程语言 网络编程
mysql降序索引和减轻索引扫描

vba foreach实例

电脑入门
vba foreach实例

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

MySQL分页模型(Pagination.php)

MySQL分页模型(Pagination.php)

在Word多图长文档中让图片编号自动更新

在Word多图长文档中让图片编号自动更新
下拉加载更多内容 ↓