简体   繁体   English

如何对我的前两列 AZ 进行排序?

[英]How can I sort my my first two columns A-Z?

I'm trying to sort Columns that contain letters and numbers.我正在尝试对包含字母和数字的列进行排序。 I want to sort the Column AZ, I've tried reading up on some functions and attempting to get it to sort however I'm getting this error:我想对列 AZ 进行排序,我尝试阅读一些函数并尝试对其进行排序,但是我收到此错误:

Private Function Sort2()
    Set HSheet = ThisWorkbook.Sheets("Building Parts")
    HBLR = HSheet.Range("A" & Rows.Count).End(xlUp).Row
    Worksheets("Building Parts").Activate

    With Range(Columns("A"), Columns("I"))
        .Sort key1:=Columns("A"), Order1:=xlDescending, Header:=xlNo
        .Sort key1:=Columns("B"), Order1:=xlDescending, Header:=xlNo
    End With
End Function

Mock Data:模拟数据:

在此处输入图片说明

Writing code from my comment as an answer so it is more readable从我的评论中编写代码作为答案,使其更具可读性


Using sort on individual columns across the same range will allow sorting xlAscending for each column, with the final sort providing the final AZ:对同一范围内的单个列使用排序将允许对每列进行 xlAscending 排序,最终排序提供最终 AZ:

Mock Data:模拟数据:

Col 1      Col 2
A          1
A          3
B          1
C          1
A          2

If you want Col1 to have primary hierarchy, it is sorted last, so:如果您希望 Col1 具有主要层次结构,则将其排在最后,因此:

With Range(Columns(1), Columns(2))
    .Sort key1:=.Columns(2), order1:=xlAscending, Header:=xlYes
    .Sort key1:=.Columns(1), order1:=xlAscending, Header:=xlYes
End With

The above code is essentially:上面的代码本质上是:

.Range(.Columns(1), .Columns(2)).Sort key1:=.Columns(1), order1:=xlAscending, key2:=.Columns(2), order2:=xlAscending, Header:=xlYes

The single line approach has the highest priority as key1 , second highest as key2 , etc.单行方法的优先级最高为key1 ,第二高为key2key2


Output after code runs would be:代码运行后的输出将是:

Col 1      Col 2
A          1
A          2
A          3
B          1
C          1

Edit (after accept):编辑(接受后):

I went back and added soem missing dot notation, found in the key1:=.Columns(1) ... unqualified references will lead to problems.我回去添加了 soem 缺少的点符号,在key1:=.Columns(1) ......不合格的引用会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM