发布网友 发布时间:2022-04-21 07:55
共1个回答
热心网友 时间:2022-06-19 04:58
在此,提供一个使用VBA编写的“冒泡程序”,供你参考:
原始数据如图:
每个单元格6个数字,以逗号(,)分隔
VBA代码如下:
Sub
单元格排序()
'
Dim
shuzu(1
To
6)
hs
=
1
ls
=
1
dachuan
=
Cells(hs,
ls)
Do
While
Len(dachuan)
<>
0
zs
=
1
Do
While
zs
<=
6
fgf
=
InStr(dachuan,
",")
If
fgf
<>
0
Then
shuzu(zs)
=
Left(dachuan,
fgf
-
1)
dachuan
=
Right(dachuan,
Len(dachuan)
-
fgf)
Else
shuzu(zs)
=
dachuan
End
If
Cells(hs,
ls
+
1)
=
shuzu(zs)
ls
=
ls
+
1
zs
=
zs
+
1
Loop
jh
=
1
Do
While
jh
<
6
If
Val(shuzu(jh))
>
Val(shuzu(jh
+
1))
Then
lssz
=
shuzu(jh)
shuzu(jh)
=
shuzu(jh
+
1)
shuzu(jh
+
1)
=
lssz
jh
=
1
Else
jh
=
jh
+
1
End
If
Loop
Cells(hs,
ls
+
2)
=
shuzu(1)
&
","
&
shuzu(2)
&
","
&
shuzu(3)
&
","
&
shuzu(4)
&
","
&
shuzu(5)
&
","
&
shuzu(6)
zs
=
1
hs
=
hs
+
1
ls
=
1
dachuan
=
Cells(hs,
ls)
Loop
'
End
Sub
见图:
这段代码执行后的效果,见图: