Excel VBA中实现数组排序的多种方法
时间:2023-12-14 20:52:12
VBA里面没有现成的Sort方法可以使用,在ExcelVBA里面要对数组进行排序,现有的通常做法:
1,通过单元格赋值以后利用工作表里的Sort方法进行排序,
2,通过SQL实现,也需要调用单元格区域存放数据,
3,直接写循环语句通过算法来实现。
除了上述方法以外,借助一些其他语言工具与VBA相结合,也能利用现成的排序功能来实现数组排序,而不需要借助表格。
例如JavaScript:
JavaScript里面也有Sort方法,可以拿来现成使用,示例代码如下:
Sub 文本升序()
Set js = CreateObject("msscriptcontrol.scriptcontrol")
js.Language = "javascript"
arr = Application.Transpose(Range("A1:A10"))
temp = Join(arr, ",")
js.addcode "function aa(bb){js=bb.split(‘,’);js.sort();return js;}"
sortarr = js.eval_r("aa(‘" & temp & "’)")
Debug.Print sortarr
End Sub
Sub 文本降序()
Set js = CreateObject("msscriptcontrol.scriptcontrol")
js.Language = "javascript"
arr = Application.Transpose(Range("A1:A10"))
temp = Join(arr, ",")
js.addcode "function aa(bb){js=bb.split(‘,’);js.sort();js.reverse();return js;}"
sortarr = js.eval_r("aa(‘" & temp & "’)")
Debug.Print sortarr
End Sub
Sub 数值升序()
Set js = CreateObject("msscriptcontrol.scriptcontrol")
js.Language = "javascript"
arr = Application.Transpose(Range("A1:A10"))
temp = Join(arr, ",")
js.addcode "function aa(bb){js=bb.split(‘,’);js.sort(function(a,b){return a-b;});return js;}"
sortarr = js.eval_r("aa(‘" & temp & "’)")
Debug.Print sortarr
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
win10中怎么让qq始终以管理员身份运行程序?
![](https://img.aspxhome.com/file/2023/28/a246372_0s.jpg)
Windows Server 2008 回收站无法清空故障的解决方法
![](https://img.aspxhome.com/file/2023/29/a259865_0s.jpg)
excel中查找快捷键是哪个?
excel表格可以复制到word文档吗怎么复制
Windows7系统管理无线网络图文教程
![](https://img.aspxhome.com/file/2023/29/a252604_0s.jpg)
三星Samsung笔记本电脑开机进入BIOS的方法与BIOS设置全功能菜单(F2)
XLUEOPS.exe是什么进程?彻底删除XLUEOPS.exe进程加快系统运行速度的方法
![](https://img.aspxhome.com/file/2023/6/a320654_0s.png)
怎么制作自己的pe系统?石大师u盘启动盘制作教程
![](https://img.aspxhome.com/file/2023/1/a272307_0s.png)
Win8系统中音频设备属性应该如何查看?
![](https://img.aspxhome.com/file/2023/2/a286300_0s.jpg)
怎么解决Win10 20H2微软账号登录一直转圈进不去系统?
![](https://img.aspxhome.com/file/2023/1/a278924_0s.jpg)
PS通道抠图的方法
![](https://img.aspxhome.com/file/2023/9/a340710_0s.jpg)
Win8.1设置滑动屏幕实现关机的解决方法
![](https://img.aspxhome.com/file/2023/4/a308712_0s.jpg)
Win10底部任务栏无响应重启无效怎么处理?
![](https://img.aspxhome.com/file/2023/1/a274845_0s.jpg)
为什么wps表格编辑完后会变大
![](https://img.aspxhome.com/file/2023/5/a164095_0s.jpg)
word中怎么设置不同的页眉
Mac系统中Apache Tomcat安装配置
![](https://img.aspxhome.com/file/2023/7/a215167_0s.png)
如何一次性复制粘贴Word文档到Excel2007
![](https://img.aspxhome.com/file/2023/3/19733_0s.png)
序列号不正确无法安装Win2003 SP1 解决办法
win7系统访问不了ftp的七种解决方法
![](https://img.aspxhome.com/file/2023/5/a317031_0s.jpg)
电脑黑屏只有鼠标怎么办?电脑黑屏只有鼠标的解决方法
![](https://img.aspxhome.com/file/2023/1/a276960_0s.png)