简体   繁体   English

使用 VBA 对 excel 中的每个单元格进行排序

[英]sorting every cell with data in excel using VBA

I recorded a macro for sorting this sheet.我录制了一个宏来对这张表进行排序。 how do I replace the hardcoded ranges so that this script works with any number of rows?如何替换硬编码范围,以便此脚本适用于任意数量的行?

Worksheets(1).sort.SortFields. _
    Clear
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("A2:A130008"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("B2:B130008"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("C2:C130008"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("D2:D130008"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("E2:E130008"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("P2:P130008"), SortOn:=xlSortOnValues, Order:= _
    xlDescending, DataOption:=xlSortNormal
Worksheets(1).sort.SortFields. _
    Add2 Key:=Range("Q2:Q130008"), SortOn:=xlSortOnValues, Order:= _
    xlDescending, DataOption:=xlSortNormal
With Worksheets(1).sort
    .SetRange Range("A1:R130008")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

This ought to do the job.这应该可以完成工作。 Please try it.请试一试。

Sub StackOverflow()

    Dim Rng         As Range            ' range to sort
    
    With Worksheets(1)
        Set Rng = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp)) _
                  .Resize(, .Columns("R").Column)
        ' .Columns("R").Column = 18 and may be replaced with this number
        
        With .Sort.SortFields
            .Clear
            ' specify the columns to sort on in sequence of priority
            .Add2 Key:=Rng.Cells(1), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Add2 Key:=Rng.Cells(2), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Add2 Key:=Rng.Cells(3), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Add2 Key:=Rng.Cells(4), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Add2 Key:=Rng.Cells(5), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Add2 Key:=Rng.Cells(16), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Add2 Key:=Rng.Cells(17), SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
        End With
        With .Sort
            .SetRange Rng
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub

Note that in .Sort.SetRange the entire range must be specified whereas for setting each of the Keys just one cell is required to identify a column.请注意,在.Sort.SetRange中,必须指定整个范围,而要设置每个键,只需要一个单元格来标识一列。 Since the cells in a range are numbered 1 and up first across and then down the first 18 cell numbers in the range coincide with the column numbers in the underling sheet range.由于范围内的单元格编号为 1,并且先向上然后向下编号,因此该范围内的前 18 个单元格编号与下表范围中的列号一致。 So Key:=Rng.Cells(1) happens to be Worksheets(1).Cells(2, "A") based on how Rng was specified.所以Key:=Rng.Cells(1)恰好是Worksheets(1).Cells(2, "A")基于Rng的指定方式。 I chose the shorter syntax but the effect is that the entire range will first be sorted on column A before other keys are applied.我选择了较短的语法,但效果是整个范围将首先在 A 列上排序,然后再应用其他键。

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

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