动态获取最后一列字符并传递它

时间:2015-10-15 06:31:23

标签: excel excel-vba vba

Excel VBA:我正在尝试动态获取最后一个列字符并将其作为最后一列传递,同时选择排序范围。但这似乎不适用于代码

Sub Sort_THAT_IS_NOT_CALLED_SORT_BECAUSE_THAT_IS_A_RESEVED_WORD()
    Dim lastrowcheck As Long, n1 As Long, LastRowcheck1 As Long, n2 As Long
    Dim lcol As Integer, colletter As String

    With Worksheets("MergeSheet")
        lastrowcheck = .Range("A" & .Rows.Count).End(xlUp).Row
        For n1 = 2 To lastrowcheck
            If .Cells(n1, 1).Value = "FUND" Then
                .Rows(n1).Delete
            End If
        Next n1

        ActiveWorkbook.Worksheets("MergeSheet").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("MergeSheet").Sort.SortFields.Add Key:=Range( _
            "A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("MergeSheet").Sort
            lcol = Cells(1, Columns.Count).End(xlToLeft).Column
            colletter = ConvertToLetter(lcol)
            .SetRange Range("A2:colletter" & lastrowcheck)
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

记录的Range.Sort method非常详细。您通常可以将录制的代码削减到记录的一小部分,并最终得到实际需要的内容。

Sub Sort_THAT_IS_NOT_CALLED_SORT_BECAUSE_THAT_IS_A_RESEVED_WORD2()
    With Worksheets("MergeSheet")
        With .Cells(2, 1).CurrentRegion
            With .Resize(.Rows.Count + (.Rows(1).Row = 1), .Columns.Count).Offset(Abs(.Rows(1).Row = 1), 0)
                .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
                            Orientation:=xlTopToBottom, Header:=xlNo, _
                            DataOption:=xlSortNormal, MatchCase:=False
        End With
    End With
End Sub