excel将工作表按笔画或拼音顺序排序
时间:2022-12-31 17:18:00
Excel中没有内置工作表排序的命令或方法,我们可以用VBA来实现工作表排序。下面的VBA代码可以将工作表按其名称的拼音或笔画的顺序来排序,同时还可以指定升序或降序。
Sub SortWorksheets()
Dim SortOrd, SortM, ActiveSht As String
Dim NumSht()
ActiveSht = ActiveWorkbook.ActiveSheet.Name
On Error Resume Next
n = Sheets.Count
If n = 1 Then
MsgBox "只有一张工作表,无需排序!"
End
End If
ReDim NumSht(1 To n)
For i = 1 To n
NumSht(i) = Sheets(i).Name
Next
‘在此设置工作表排序方法和排序方向
‘SortM = xlPinYin为按拼音顺序, SortM = xlStroke为按笔画顺序
‘SortOrd = xlAscending为升序,SortOrd = xlDescending为降序
SortOrd = xlAscending
SortM = xlPinYin
Set sht = Sheets.Add
sht.Move after:=Sheets(n + 1)
sht.Visible = False
With sht.Range("A1:A" & n)
.NumberFormat = "@"
.Value = Application.WorksheetFunction.Transpose(NumSht())
.Sort Key1:=sht.Range("A1"), Order1:=SortOrd, SortMethod:=SortM
NumSht() = Application.WorksheetFunction.Transpose(.Value)
End With
For i = 1 To n
Sheets(NumSht(i)).Move Before:=Sheets(i)
Next
Application.DisplayAlerts = False
sht.Delete
Application.DisplayAlerts = True
ActiveWorkbook.Worksheets(ActiveSht).Select
End Sub
代码中的SortOrd变量指定工作表的排序次序,xlAscending为升序,xlDescending为降序。SortM变量指定工作表的排序方法,xlPinYin指定工作表按拼音顺序排序,xlStroke为按笔画顺序排序。在代码中进行相应的更改即可按不同的方法和次序进行排序。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
鼠标右键不能新建文件夹提示错误代码16389的解决方法
![](https://img.aspxhome.com/file/2023/3/a292056_0s.jpg)
Word文档技巧—一分钟学会排版分栏
win10开机后桌面只剩回收站_桌面图标只剩回收站的恢复方法
![](https://img.aspxhome.com/file/2023/4/a309999_0s.png)
用word简单制作组织架构图
![](https://img.aspxhome.com/file/2023/7/27567_0s.jpg)
WPS移动版“加密”各种文档的方法步骤
![](https://img.aspxhome.com/file/2023/3/a167023_0s.png)
win11任务栏透明设置方法
![](https://img.aspxhome.com/file/2023/1/a279801_0s.png)
win8/win8.1升级win10要收费吗?win8/win8.1升级win10教程
![](https://img.aspxhome.com/file/2023/29/a252197_0s.png)
excel如何保留两位小数
excel表格设置下拉框的方法
Excel中表格跳过复制隐藏区域单元格的操作方法
Win10 21H1系统升级的方法
![](https://img.aspxhome.com/file/2023/1/a278317_0s.jpg)
联通机顶盒错误代码9999/54001决绝方案
![](https://img.aspxhome.com/file/2023/9/a340998_0s.jpg)
word 2016中如何创建SmartArt图形
![](https://img.aspxhome.com/file/2023/4/32794_0s.jpg)
十大技巧 让你的搜狗输入法十全十美
![](https://img.aspxhome.com/file/2023/5/a318172_0s.jpg)
Word中创建样式的攻略
![](https://img.aspxhome.com/file/2023/8/25008_0s.png)
同步而变 Excel智能照相机功能介绍
![](https://img.aspxhome.com/file/2023/2/41072_0s.jpg)
win10电脑遇到蓝屏重启修复办法
![](https://img.aspxhome.com/file/2023/1/a281324_0s.png)
如何将PPT内容导出为JPG图片
Win10 RS3 16176增加一项新功能:让高CPU功耗暴降11%
![](https://img.aspxhome.com/file/2023/28/a242215_0s.jpg)
Microsoft edge自动翻译怎么打开?开启edge浏览器自动翻译的方法
![](https://img.aspxhome.com/file/2023/7/a328206_0s.jpg)