Excel VBA - 如何按单元格的前两个字母排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21487727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:40:10  来源:igfitidea点击:

Excel VBA - How to sort by first two letters of cell?

excelvbasorting

提问by Josh

I have two columns I want to sort. I want it to look exactly like below with Column A first sorted by its first two letters, then by Column B.

我有两列要排序。我希望它看起来与下面完全一样,A 列首先按前两个字母排序,然后按 B 列排序。

Column A     -    Column B
AB - Info         3339876
AB - Data         3339877  
AB - Data         3339878 
AC - Info         3339123
AC - Data         3339124
AC - Info         3339125
AD - Info         3339456
AD - Info         3339457
AD - Data         3339458

The first two letters of Column A are the MOST important and must be sorted by them first. The information after the first two letters of Column A is irrelevant and does not matter. It is much more important that the Column B # data to be sorted in ascending order second (after the first two letters of column A)

A 列的前两个字母是最重要的,必须先按它们排序。A列前两个字母后的信息无关紧要。更重要的是B列数据要按升序排列第二(在A列的前两个字母之后)

Sorry for the confusion. Hopefully that clears things up.

对困惑感到抱歉。希望这能解决问题。

回答by KyleMit

Sort normally by highlighting the column and Hit Alt+ A+ SA

通过突出显示列和 Hit Alt+ A+ 来正常排序SA



Update:

更新:

'Excel sorts alphanumeric text left to right, character by character', so it may not properly order your numbers if combined into a single cell. You should create an extra column with just the two letter code that you want to use for sorting by using =LEFT(A2,2)and copying all the way down. Then do a multilayered sort by clicking the sort button, sorting first on the two digit code alphabetically and second on the number column:

'Excel sorts alphanumeric text left to right, character by character',因此如果合并到一个单元格中,它可能无法正确排序您的数字。您应该创建一个仅包含两个字母代码的额外列,您希望通过使用=LEFT(A2,2)和复制一路向下进行排序。然后通过单击排序按钮进行多层排序,首先按字母顺序对两位数代码进行排序,然后按数字列排序:

example

例子

回答by Gary's Student

Select the cells and sort normally.

选择单元格并正常排序。

回答by kamila

the answer above is not excatly correct, it will work until all of the numbers has the same length. but if we have

上面的答案并不完全正确,它会起作用,直到所有数字的长度都相同。但如果我们有

AA 111
AA  99

then excel sorts it like this:

然后excel这样排序:

AA111
AA99

(we want the other way around). you should use the Leftfunction as mentioned, but alone, just to create a column with two first letters from the first one. then you have to use the custom sorthttp://office.microsoft.com/en-us/excel-help/sort-data-using-a-custom-list-HA102809333.aspxto sort fisrt by the new column and then by the column with numbers

(我们想要相反的方式)。您应该使用Left提到的函数,但单独使用,只是为了创建一个包含第一个字母的两个第一个字母的列。那么你必须使用custom sorthttp://office.microsoft.com/en-us/excel-help/sort-data-using-a-custom-list-HA102809333.aspx按新列然后按带数字的列