排序 Excel 文件的 4 个键

Sorting 4 Keys of Excel file

Excel 文件 (2013) 中要求对 4 个关键字段进行排序。下面给出的是代码。但没有得到想要的结果。需要您的帮助来解决问题。

Sub ToSortFile()
    Dim lastRow, LastCol As Long

    lastRow = Cells(Rows.Count, 2).End(xlUp).Row
    LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
    Set source_data = Range(Cells(2, 2), Cells(lastRow, LastCol))
    Set Ws = Sheets("Details")
    With Ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Ws.Range("B3"), Order:=xlAscending
        .SortFields.Add Key:=Ws.Range("C3"), Order:=xlAscending
        .SortFields.Add Key:=Ws.Range("K3"), Order:=xlAscending
        .SortFields.Add Key:=Ws.Range("M3"), Order:=xlAscending
        .SetRange Ws.Range("B3:U" & lastRow)
        .Header = xlYes
        .Apply
    End With
End Sub

对范围进行排序

  • 使用 Option Explicit 将强制您声明所有变量(Dim ws As WorksheetDim source_data As Range)。

  • 不推荐在一行中声明变量,但如果你不能帮助它,你需要正确地做:

    Dim LastRow As Long, LastCol As Long, i As Long, Num As Double
    

    因为在Dim lastRow, LastCol As Long中,LastRow被声明为As Variant


  • 当您写 Set ws = Sheets("Details") 时,Sheets 不合格,即它指的是活动工作簿 (ActiveWorkbook),您正在查看的工作簿可能是正确的工作簿,也可能不是。

  • 如果您正在处理的工作sheet在包含代码的工作簿中,那么您想使用ThisWorkbook:

    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Details")
    
  • 如果不是,那么你想通过以下方式对每个作品sheet进行限定:

    Dim wb As Workbook: Set wb = Workbooks("Test.xlsx")
    

    或者如果工作簿已关闭,打开它...

    Dim wb As Workbook: Set wb = Workbooks.Open("C:\Test\Test.xlsx")
    

    ...并继续...

    Dim ws As Worksheet: Set ws = wb.Worksheets("Details")  
    
  • 请注意,有些情况下您必须使用 ActiveWorkbook


  • 当您写 lastRow = Cells(Rows.Count, 2).End(xlUp).Row 时,CellsRows 不合格,即它们指的是活动 sheet(ActiveSheet ) 可能有效sheet Details 也可能无效。

  • 要限定它,您可以使用以下内容:

    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Details")
    Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row      
    

  • 一个经验法则是尽可能明确(合理),即不要让 Excel 为您决定。
  • 您不会使用 .Header = xlGuess,但当范围包含 header 时,您将使用 xlYes,如果不包含,则使用 xlNo。在您的特定情况下,您仅引用数据 (Ws.Range("B3:U" & lastRow)),即它不包含 header,因此您应该使用 xlNo.
  • Keys 参数相关,尽管指定要排序的列中的任何单元格就足够了,但我建议指定整个列,即不要使用 B2 使用 B2:BLastRow, 2 是 header 行。

  • 请注意,排序是按照从最后添加的排序字段到第一个的顺序执行的,即在您的情况下,顺序是 MKCB 即它对你来说仍然可能是错误的。
Option Explicit

Sub ToSortFile()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim ws As Worksheet: Set ws = wb.Worksheets("Details")
    
    Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    Dim LastCol As Long
    LastCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
    
    Dim srg As Range: Set srg = ws.Range("B2", ws.Cells(LastRow, LastCol))
    
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=srg.Columns(1), Order:=xlAscending
        .SortFields.Add Key:=srg.Columns(2), Order:=xlAscending
        .SortFields.Add Key:=srg.Columns(10), Order:=xlAscending
        .SortFields.Add Key:=srg.Columns(12), Order:=xlAscending
        .SetRange srg
        .Header = xlYes
        .Apply
    End With

End Sub