我试图让一个宏在报告中按升序显示信息

时间:2016-03-24 13:18:07

标签: excel vba excel-vba

我不确定这有什么问题,它不会因某些原因而起作用!有任何想法吗?我只想拍摄粘贴的内容,并确保它按升序排列最少,数量最多。

'Range("A:N").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
    'xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    'DataOption1:=xlSortTextAsNumbers

1 个答案:

答案 0 :(得分:0)

我认为您遇到的问题是您正在对entire column进行排序,而不是定义数据范围的结尾。

见下文:

Dim ws as Worksheet
Set ws = Worksheets("Sheet1") 'change as needed

With ws

    Dim lRow as Long
    lRow = .Range("A" & .Rows.Count).End(xlUp).Row

    .Range("A1:N" & lRow).Sort Key1:=.Range("A1:A" & lRow), _ 
        Order1:=xlAscending, Header:= xlGuess, OrderCustom:=1, _ 
        MatchCase:=False, Orientation:=xlTopToBottom, _ 
        DataOption1:=xlSortTextAsNumbers

End With

如果你有XL 2007或更高版本,这可能是要走的路:

Dim ws as Worksheet
Set ws = Worksheets("Sheet1") 'change as needed

With ws

    Dim lRow as Long
    lRow = .Range("A" & .Rows.Count).End(xlUp).Row

    With .Sort

        With .SortFields

            .Clear
            .Add Key:=.Range("A1:A" & lRow), SortOn:=xlValues, _
                 Order:=xlAscending, DataOption:=xlSortNormal
        End With

        .SetRange .Range("A1:N" & lRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

   End With

End With