简体   繁体   English

根据“ L”列以升序对范围进行排序

[英]Sort a range based on column “L” in ascending order

I use the following macro recorded to sort Column L in Ascending order, but it seems a bit ridiculous to have to use 12 lines of code to sort something. 我使用下面记录的宏按升序对L列进行排序,但是必须使用12行代码对某些内容进行排序似乎有点荒谬。 Is there a more "efficient" way to do this? 有没有更“有效”的方法来做到这一点?

I need it to sort the entire sheet, based on column L. 我需要它根据L列对整个工作表进行排序。

Sheets("Sheet1").Range("A1").End(xlToRight).AutoFilter
Sheets("Sheet1").AutoFilter.Sort.SortFields.Clear
Sheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
    ("L1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortTextAsNumbers
With Sheets("Sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

The following line will sort the L column in ascending order. 下一行将按升序对L列进行排序。

 Sheets("Sheet1").Sort Key1:=Range("L1"), Order1:=xlAscending

This will sort the ENTIRE sheet, to sort just the data, I'd declare a couple variables like below: 这将对整个工作表进行排序, 仅对数据进行排序,我将声明以下几个变量:

Dim lastRow As Long, lastColumn As Long, StartCell AS Range, s As Worksheet

Set s = Sheets("Sheet1")
Set StartCell = Range("A1")
lastRow = s.Cells(s.Rows.Count, StartCell.Column).End(xlUp).Row
lastColumn = s.Cells(StartCell.Row, s.Columns.Count).End(xlToLeft).Column

s.Range(StartCell, s.Cells(lastRow, lastColumn)).Sort _
 Key1:=Range("L1"), Order1:=xlAscending

Edited to reflect inclusion of ENTIRE sheet 编辑以反映整个工作表的内容

If your data is starting in Row 1 and will stay in Row 1, you can just use this: 如果您的数据从第1行开始并将保留在第1行,则可以使用以下命令:

Columns.Sort key1:=Range("L1"), order1:=xlAscending

This will sort all columns based on the values in column L1. 这将基于列L1中的值对所有列进行排序。

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

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