以编程方式对列表列进行排序

时间:2013-11-20 06:59:29

标签: excel vba sorting

我正在尝试以编程方式对excel表进行排序,以下是我正在使用的代码。我想知道如何使用以下方法为已排序列设置背景颜色。谢谢!

Private Sub cmdSort_Click()

    Dim loTable As ListObject
    Dim so As Sort
    Dim lcColumnName As String
    Dim lnOrder As Integer

    If optAsc.Value Then
        lnOrder = xlAscending
    Else
        lnOrder = xlDescending
    End If
    lcColumnName = Trim(cboColumns.Value)
    Set loTable = ActiveSheet.ListObjects(ActiveSheet.Name)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    UnProtectAllWrkSheet
    Application.EnableCancelKey = xlDisabled 'enable ESC key
    With loTable.Sort
        With .SortFields
            .Clear
            If lcColumnName <> "" And Not IsEmpty(lcColumnName) And lcColumnName <> "+ALL" Then
                .Add Range(Replace(loTable.Name, " ", "_") & "[[#All], [" &lcColumnName & "]]"), SortOn:=xlSortOnValues, Order:=lnOrder
            End If
        End With
        .Header = xlYes
        .Orientation = xlSortColumns
        .Apply
    End With
    ProtectAllWrkSheet
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.EnableCancelKey = xlInterrupt 'enable ESC key
    Unload Me
 End Sub

0 个答案:

没有答案