0

不知何故,我没有得到排序键的东西。我浏览了网上的一些例子。它们看起来很清楚,但我不明白您如何准确地将键分配给某个列...我的代码应该从 A2:S 中全部排序。第一个条件是 F 列,然后按 G 列排序。示例:

F2: A    G2:B
F3: A    G3:C
F4: A    G4:A

应该变成:

F2: A    G2:A
F3: A    G3:B
F4: A    G4:C

到目前为止我得到的代码:

Private Sub ButtonExport_Click()

    Dim sKey1 As String
    Dim sKey2 As String
    Dim sKey3 As String

    sKey1 = Worksheets("Variable Tags").Range("lblSortByKey1").Value
    sKey2 = Worksheets("Variable Tags").Range("lblSortByKey2").Value

    If sKey2 = "" Then
        Call SortTable(sKey1, , , "tblTagLists_All")
        MsgBox "Sorting done based on one column"
    Else
        Call SortTable(sKey1, sKey2, , "tblTagLists_All")
        MsgBox "Sorting done based on two columns"
    End If

End Sub
4

1 回答 1

1

尝试录制宏,然后组织代码。

Sub NinjaSort()
    Dim theRange As Range

    Set theRange = Range("F1:G6")

    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=theRange.Columns(1).Cells, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=theRange.Columns(2).Cells, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange theRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub
于 2013-10-28T14:12:08.670 回答