按范围排序

时间:2018-11-29 20:26:47

标签: excel vba excel-vba

我有这个并且它可以工作,但是我需要它在特定范围内进行排序。它不一定总是向下39行。

   ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("L12:L39") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal

所以我做到了,但是仍然行不通。

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("L12", Selection.End(xlDown)) _
    , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
    "AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal

我在做什么错?我认为这可能很简单,但我只是没有看到。

这是我该部分的完整代码。

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("L12", Selection.End(xlDown)) _
    , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
    "AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("J12", Selection.End(xlDown)) _
    , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("B11", Selection.End(xlDown))
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply

它表示1004排序参考无效。

'*** Sorting by payment method
    Range("L12").Select                                                     '*** Check this sorting for the correct cells
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("L12", Selection.End(xlDown)) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("J12", Selection.End(xlDown)) _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal


    Dim sht2 As Worksheet
    Dim LastRow2 As Long
    Dim LastColumn2 As Long
    Dim StartCell2 As Range
    Set sht2 = Worksheets("Sheet1")
    Set StartCell2 = Range("B11")
    Worksheets("Sheet1").UsedRange
    LastRow2 = sht2.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastColumn2 = StartCell.SpecialCells(xlCellTypeLastCell).Column
    sht2.Range(StartCell2, sht2.Cells(LastRow2, LastColumn2)).Select

    With ActiveWorkbook.Worksheets("Sheet1").Sort
        '.SetRange(StartCell2, sht2.Cells(LastRow2, LastColumn2)).Select
        '.SetRange(StartCell2, sht2.Cells(LastRow2, LastColumn2)).Select
        '.SetRange Range("B11", LastRow2)
        .SetRange Range("B11:AA" & LastRow2)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

谢谢。

1 个答案:

答案 0 :(得分:2)

您应该看一下这些功能。我经常使用它们,并且将它们保存在我的Personal Workbook中,以便快速,轻松地进行引用。

Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
    With ws
        lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
End Function

Function LastColumn(ws As Worksheet, Optional rowNum As Long = 1)
    With ws
        LastColumn = .Cells(rowNum, .Columns.Count).End(xlToLeft).Column
    End With
End Function

结合以上功能,您可以使用此

Dim ws As Worksheet
Dim sortRng As Range, rngL As Range, rngJ As Range
Dim lstRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lstRow = lastRow(ws, "J")

With ws

    Set sortRng = .Range("B11", .Cells(lstRow, LastColumn(ws, 11)))
    Set rngJ = .Range("J11:J" & lstRow)
    Set rngL = .Range("L11:L" & lstRow)

    With .Sort
        .SortFields.Clear
        .SortFields.Add Key:=rngJ, SortOn:=xlSortOnValues, Order:=xlAscending, _
                CustomOrder:="AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal
        .SortFields.Add Key:=rngL, SortOn:=xlSortOnValues, Order:=xlDescending, _
                DataOption:=xlSortNormal
        .SetRange sortRng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End With
  

注意:以上代码中的任何地方均不使用.Select 。从此开始并学习avoid using select,因为它很少需要。


分解代码

您将使用此行设置整个排序区域:

Set sortRng = .Range("B11", .Cells(lstRow, LastColumn(ws, 11)))

这些行是您要排序的特定列的范围。

Set rngJ = .Range("J11:J" & lstRow)
Set rngL = .Range("L11:L" & lstRow)

确保您清除所有先前的条件

.SortFields.Clear

然后添加您的条件(例如,您的自定义排序)

.SortFields.Add Key:=rngJ, SortOn:=xlSortOnValues, Order:=xlAscending, _
    CustomOrder:="AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal
.SortFields.Add Key:=rngL, SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal