在列vba中按日期排序

时间:2012-12-20 18:41:26

标签: vba date sorting excel-vba autofilter

我试图让我的数据按N列中出现的日期对每一行进行排序。现在它只是通过istelf对n列进行排序而不改变任何其他信息。 如何重新编写代码以使整行移动而不仅仅是该行中的单个单元格?

 Sub SortByDate()
 Dim rSortRange As Range
 Dim ws As Worksheet

 Set ws = Sheets("Copy")
 Set rSortRange = ws.Range("N11", "N111")
 rSortRange.Sort Key1:=ws.Range("N11"), Order1:=xlAscending, _
            Key2:=Range("N20"), Order2:=xlAscending, _
            Key3:=Range("N29"), Order3:=xlAscending, _
            Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
            DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
 End Sub

使用以下宏代码

 Sub SortByDate()
 Dim rSortRange As Range
 Dim ws As Worksheet

 Set ws = Sheets("Copy")
 Set rSortRange = ws.Range("N11", "N111")
 rSortRange.Sort Key1:=ws.Range("N11"), Order1:=xlAscending, _
            Key2:=Range("N20"), Order2:=xlAscending, _
            Key3:=Range("N29"), Order3:=xlAscending, _
            Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
            DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
 End Sub

有时我会忘记记录一个宏是多么容易!

1 个答案:

答案 0 :(得分:1)

  Sub SortByDate()
  Dim rSortRange As Range
  Dim ws As Worksheet

  Set ws = Sheets("Copy")
  Set rSortRange = ws.Range("N11", "N111")
  rSortRange.Sort Key1:=ws.Range("N11"), Order1:=xlAscending, _
        Key2:=Range("N20"), Order2:=xlAscending, _
        Key3:=Range("N29"), Order3:=xlAscending, _
        Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
        DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
  End Sub