excel,使用VBA将选中单元格按字符长度横向排序?

如图一,将表中选中的单元格按字符长度横向排序,比如我将选定的内容按3列(或者4列,5列。。),按字符长度横向排序。结果如图2。谢谢大佬

Excel 中可以使用 VBA 代码来实现将选中单元格按字符长度横向排序。
以下是 VBA 代码:
Sub SortSelectedCellsByLength() 'Declare a variable to store the range of selected cells Dim rng As Range
goCopy code
'Assign the selected cells to the variable
Set rng = Selection

'Sort the selected cells by the length of their values
rng.Sort Key1:=rng, Order1:=xlAscending, _
Key2:=rng, Order2:=xlAscending, _
Orientation:=xlLeftToRight, Header:=xlNo
End Sub
这段代码会将选中的单元格按字符长度从小到大排序。要使用此代码,请打开 Excel,按“Alt + F11”打开 VBA 编辑器,并在模块中粘贴以上代码。接下来,保存代码并在 Excel 中选择需要排序的单元格,然后运行代码。

追问

排出来的结果不对

温馨提示:答案为网友推荐,仅供参考
相似回答