在1个工作簿中排序工作表

时间:2018-06-14 10:13:21

标签: excel vba excel-vba

我在网上搜索了一个宏,它可以帮助我对工作簿中的工作表进行排序并对其进行一些修改(添加排除工作表)

Sub SortDataWorksheets()

Dim wsh As Worksheet
For Each wsh In ThisWorkbook.Sheets
    If wsh.Name <> "Dashboard" And wsh.Name <> "rawdata" And wsh.Name <> "template" And wsh.Name <> "macros instructions" And wsh.Name <> "Sheet1" _
    And wsh.Name <> "Sheet2" And wsh.Name <> "inputlist" And wsh.Name <> "ProductList" And wsh.Name <> "NA" Then

    'sort columns A to AL based on data in column B
    wsh.Columns("A:AL").Sort key1:=Range("B3"), order1:=xlAscending, Header:=xlYes
    End If
Next

End Sub

但是,这不起作用,因为excel会抛出

Run Time error '1004' :
The sort reference is not valid. Make sure that it's within the data you want to sort...

我的数据从第3行开始,前2行是标题。如何排除前两行进行排序?

3 个答案:

答案 0 :(得分:1)

更改自:

wsh.Columns("A:AL").Sort key1:=Range("B3"), order1:=xlAscending, Header:=xlYes

要:

wsh.Columns("A:AL").Sort key1:=wsh.Range("B3"), order1:=xlAscending, Header:=xlYes

因为如果您不引用父工作表,VBA将ActiveSheet或代码所在的工作表作为父工作表。两者都会在你的情况下返回一个错误。

答案 1 :(得分:0)

这对我有用:

Sub SortDataWorksheets()

    Dim wsh As Worksheet
    Dim LastRow As Long

    For Each wsh In ThisWorkbook.Sheets
        With wsh
            If .Name <> "Dashboard" And .Name <> "rawdata" And .Name <> "template" And _
                .Name <> "macros instructions" And .Name <> "Sheet1" _
                And .Name <> "Sheet2" And .Name <> "inputlist" And _
                .Name <> "ProductList" And .Name <> "NA" Then

                LastRow = .Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row

                'sort columns A to AL based on data in column B
                .Range("A2:AL" & LastRow).Sort key1:=.Range("B3"), order1:=xlAscending, Header:=xlYes
            End If
        End With
    Next

End Sub

请注意使用特定范围而不是列。

答案 2 :(得分:0)

在忽略工作表时,我发现更容易阅读Select Case而不是多个IF..AND..THEN

下面的代码将调整为包含B列数据的行数。

我还不确定哪种方法是首选的排序方式 - 单行,或宏录制器返回的内容(类似于下面的内容)。

Public Sub SortDataWorksheets()

    Dim wsh As Worksheet
    Dim lLastRow As Long

    For Each wsh In ThisWorkbook.Worksheets
        Select Case wsh.Name
            Case "Dashboard", "rawdata", "template", "macros instructions", _
                 "Sheet1a", "Sheet2a", "inputlist", "ProductList", "NA"

                 'Do nothing

            Case Else

                lLastRow = wsh.Cells(wsh.Rows.Count, 2).End(xlUp).Row

                With wsh.Sort

                    With .SortFields
                        .Clear
                        .Add Key:=Range("B5:B" & lLastRow), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
                    End With

                    .SetRange Range("A5:C" & lLastRow)
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    '.SortMethod = xlPinYin 'Only need if sorting Chinese characters.
                    .Apply

                End With

        End Select
    Next wsh

End Sub