循环遍历多个工作表和表以执行多标准排序

时间:2015-05-26 22:30:29

标签: excel vba excel-vba sorting

尝试创建一个遍历多个工作表的循环,选择表并根据多个条件对其进行排序。每个工作表只有一个表,这是我到目前为止所提出的。

    Sub sorter()
'
' sorter Macro
'
' Keyboard Shortcut: Ctrl+Shift+F
'

Dim varTblSortName As Variant
varTblSortName = ActiveSheet.Name
Dim Client As Worksheet
Dim rng As Range



For Each Client In ActiveWorkbook.Worksheets
'This tests to make sure the code is only run on Client_ sheets
    If InStr(1, Client.Name, "Client_", vbTextCompare) Then
  Set rng = ActiveSheet.ListObjects(1)


     Worksheets(Client.Name).Activate
        'ActiveSheet.ListObjects(1).Range.Select

            'Range(, Selection.End(xlDown)).Select
            'Range(Selection, Selection.End(xlToRight)).Select
            'ActiveWorkbook.Worksheets("Client_Jamie").ListObjects("Client_Jamie").Sort. _

               ActiveSheet.ListObjects(1).Sort.SortFields.Clear
            ActiveWorkbook.ActiveSheet.ListObjects(1).Sort. _
                SortFields.Add Key:=Range("varTblSortName[Asset Class]"), SortOn:= _
                xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ActiveWorkbook.ActiveSheet.ListObjects(1).Sort. _
                SortFields.Add Key:=Range("varTblSortName[Sector]"), SortOn:=xlSortOnValues, _
                Order:=xlAscending, DataOption:=xlSortNormal
            ActiveWorkbook.ActiveSheet.ListObjects("varTblSortName").Sort. _
                SortFields.Add Key:=Range("varTblSortName[Ticker]"), SortOn:=xlSortOnValues, _
                Order:=xlAscending, DataOption:=xlSortNormal
            With ActiveWorkbook.ActiveSheet.ListObjects("varTblSortName").Sort
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            End If
        Next Client
End Sub

1 个答案:

答案 0 :(得分:0)

我已经扩展了With / End With的使用,并将你的字符串变量从引用的字符串中删除。

Sub sorter()
' Keyboard Shortcut: Ctrl+Shift+F
    Dim varTblSortName As Variant
    Dim Client As Worksheet

    For Each Client In ActiveWorkbook.Worksheets
        With Client
            If CBool(InStr(1, .Name, "Client_", vbTextCompare)) Then
                varTblSortName = .Name
                With .ListObjects(varTblSortName)
                    .Sort.SortFields.Clear
                    .Sort.SortFields.Add Key:=Range(varTblSortName & "[Asset Class]"), SortOn:= _
                        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                    .Sort.SortFields.Add Key:=Range(varTblSortName & "[Sector]"), SortOn:=xlSortOnValues, _
                        Order:=xlAscending, DataOption:=xlSortNormal
                    .Sort.SortFields.Add Key:=Range(varTblSortName & "[Ticker]"), SortOn:=xlSortOnValues, _
                        Order:=xlAscending, DataOption:=xlSortNormal
                    With .Sort
                        .Header = xlYes
                        .MatchCase = False
                        .Orientation = xlTopToBottom
                        .SortMethod = xlPinYin
                        .Apply
                    End With
                End With
            End If
        End With
    Next Client
End Sub