Excel VBA排序和过滤宏

时间:2018-03-08 20:31:42

标签: excel-vba vba excel

我非常擅长使用Excel中的宏。我使用了“录制宏”' Excel 2016中的功能,为单个列构建过滤器和排序宏,然后在代码编辑器中进行迭代。我不确定它有什么问题,但它一直让我的程序崩溃。代码如下。任何帮助都会很棒。

Sub Filter()
'
' Filter Macro
' Filter workbook
'
' Keyboard Shortcut: Ctrl+Shift+A
'

ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.Sort.SortFields. _
        Add Key:=Range("B1:B62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("Uncategorized Products").Select
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=6, Criteria1:="=0"
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=2, Criteria1:="<>"""


ActiveWorkbook.Worksheets("Missing Images").AutoFilter.Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Missing Images").AutoFilter.Sort.SortFields. _
        Add Key:=Range("B1:B62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("Missing Images").Select
        ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=5, Criteria1:="=FALSE"
        ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""

ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.Sort.SortFields. _
        Add Key:=Range("B1:B62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("RWI Settings").Select
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=2, Criteria1:="<>"""

    ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.Sort.SortFields. _
        Add Key:=Range("A1:A62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("Names & Descriptions").Select
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=5, Criteria1:="=TRUE"
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""

    ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.Sort.SortFields. _
        Add Key:=Range("A1:A62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("Disabled Items").Select
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""


    ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.Sort.SortFields. _
        Add Key:=Range("A1:A62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Sheets("Boost Scores").Select
    ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""
End Sub

修正了我的问题。粘贴在下面的新代码:

子排序() &#39; &#39;排序宏 &#39; &#39;键盘快捷键: Ctrl + Shift + A &#39;     Dim lastRow As Long

Sheets("Store Product Data").Activate
'Search for any entry, by searching backwards by Rows.
lastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

With Sheets("Uncategorized Products")
    With .Range("A2:F" & lastRow)
        .Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
        .Value = .Value
    End With
End With

    With Sheets("Missing Images")
    With .Range("A2:E" & lastRow)
        .Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
        .Value = .Value
    End With
End With

    With Sheets("RWI Settings")
    With .Range("A2:H" & lastRow)
        .Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
        .Value = .Value
    End With
End With

    With Sheets("Names & Descriptions")
    With .Range("A2:E" & lastRow)
        .Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
        .Value = .Value
    End With
End With

    With Sheets("Disabled Items")
    With .Range("A2:D" & lastRow)
        .Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
        .Value = .Value
    End With
End With

        With Sheets("Boost Scores")
    With .Range("A2:E" & lastRow)
        .Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
        .Value = .Value
    End With
End With

Sheets("Uncategorized Products").Select
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
    Add Key:=Range("B1:B50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
    Add Key:=Range("E1:E50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
    ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
    Add Key:=Range("F1:F50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

    Sheets("Missing Images").Select
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
    Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
    Add Key:=Range("D1:D50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
    ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
    Add Key:=Range("E1:E50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

        Sheets("RWI Settings").Select
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
    Add Key:=Range("B1:B50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
    Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

        Sheets("Names & Descriptions").Select
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
    Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
    Add Key:=Range("B1:B50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
        ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
    Add Key:=Range("e1:e50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

        Sheets("Disabled Items").Select
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
    Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
    Add Key:=Range("D1:D50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

        Sheets("Boost Scores").Select
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
    Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
    Add Key:=Range("D1:D50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

End Sub

0 个答案:

没有答案