Excel 如何在高级过滤器中循环通过条件?

Excel 如何在高级过滤器中循环通过条件?,excel,vba,loops,advanced-filter,Excel,Vba,Loops,Advanced Filter,我正在尝试根据条件筛选一个表,并将结果复制粘贴到其他工作表 基本上,我在一张表(“部门ERP”)中存储了大量数据,我需要根据标准筛选列(“GLO_MASS_LINE”),然后将每个结果复制并粘贴到不同的表中 由于自动筛选和随后的复制粘贴选项太慢,我决定使用高级筛选。我准备了大量的表格(从第11页到第38页),其中我想列出具体成本的详细信息(例如,我想过滤存储在“部门ERP”中的表格,用于员工教育,并将结果复制粘贴到表格(“EDUC”)=第11页),然后我想过滤“事件/关系营销”,并将结果复制粘贴

我正在尝试根据条件筛选一个表,并将结果复制粘贴到其他工作表

基本上,我在一张表(“部门ERP”)中存储了大量数据,我需要根据标准筛选列(“GLO_MASS_LINE”),然后将每个结果复制并粘贴到不同的表中

由于自动筛选和随后的复制粘贴选项太慢,我决定使用高级筛选。我准备了大量的表格(从第11页到第38页),其中我想列出具体成本的详细信息(例如,我想过滤存储在“部门ERP”中的表格,用于员工教育,并将结果复制粘贴到表格(“EDUC”)=第11页),然后我想过滤“事件/关系营销”,并将结果复制粘贴到表格中(“ERMA”)等)

我现在面临的问题是代码在表单中循环并过滤数据,但只针对第一个标准(标准仍然是第一个“员工教育”)


你能帮我找到这里的问题吗?任何帮助都将不胜感激。

我想这就是你想要的

Sub Copy_With_AutoFilter1()
'Note: This macro use the function LastRow
    Dim My_Range As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim FilterCriteria As String
    Dim CCount As Long
    Dim WSNew As Worksheet
    Dim sheetName As String
    Dim rng As Range

    'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
    'and the header of the first column, D is the last column in the filter range.
    'You can also add the sheet name to the code like this :
    'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
    'No need that the sheet is active then when you run the macro when you use this.
    Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
    My_Range.Parent.Select

    If ActiveWorkbook.ProtectStructure = True Or _
       My_Range.Parent.ProtectContents = True Then
        MsgBox "Sorry, not working when the workbook or worksheet is protected", _
               vbOKOnly, "Copy to new worksheet"
        Exit Sub
    End If

    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False

    'Firstly, remove the AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Filter and set the filter field and the filter criteria :
    'This example filter on the first column in the range (change the field if needed)
    'In this case the range starts in A so Field 1 is column A, 2 = column B, ......
    'Use "<>Netherlands" as criteria if you want the opposite
    My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands"

    'If you want to filter on a cell value you can use this, use "<>" for the opposite
    'This example uses the activecell value
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value

    'This will use the cell value from A2 as criteria
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value

    ''If you want to filter on a Inputbox value use this
    'FilterCriteria = InputBox("What text do you want to filter on?", _
     '                              "Enter the filter item.")
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria

    'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
    CCount = 0
    On Error Resume Next
    CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
    On Error GoTo 0
    If CCount = 0 Then
        MsgBox "There are more than 8192 areas:" _
             & vbNewLine & "It is not possible to copy the visible data." _
             & vbNewLine & "Tip: Sort your data before you use this macro.", _
               vbOKOnly, "Copy to worksheet"
    Else
        'Add a new Worksheet
        Set WSNew = Worksheets.Add(After:=Sheets(ActiveSheet.Index))

        'Ask for the Worksheet name
        sheetName = InputBox("What is the name of the new worksheet?", _
                             "Name the New Sheet")

        On Error Resume Next
        WSNew.Name = sheetName
        If Err.Number > 0 Then
            MsgBox "Change the name of sheet : " & WSNew.Name & _
                 " manually after the macro is ready. The sheet name" & _
                 " you fill in already exists or you use characters" & _
                 " that are not allowed in a sheet name."
            Err.Clear
        End If
        On Error GoTo 0

        'Copy/paste the visible data to the new worksheet
        My_Range.Parent.AutoFilter.Range.Copy
        With WSNew.Range("A1")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            ' Remove this line if you use Excel 97
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With

        ' If you want to delete the rows that you copy, also use this
        ' With My_Range.Parent.AutoFilter.Range
        '     On Error Resume Next
        '     Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
        '               .SpecialCells(xlCellTypeVisible)
        '     On Error GoTo 0
        '     If Not rng Is Nothing Then rng.EntireRow.Delete
        ' End With

    End If

    'Close AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Restore ScreenUpdating, Calculation, EnableEvents, ....
    My_Range.Parent.Select
    ActiveWindow.View = ViewMode
    If Not WSNew Is Nothing Then WSNew.Select
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With

End Sub


Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlValues, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function
Sub-Copy_与_AutoFilter1()
'注意:此宏使用函数LastRow
将我的_范围变暗为范围
暗淡的CalcMode与长
将视图模式变暗为长
作为字符串的Dim FilterCriteria
长帐
新建为工作表
将sheetName设置为字符串
变暗rng As范围
'在ActiveSheet上设置筛选范围:A1是筛选范围的左上角单元格
'并且第一列的标题D是筛选范围中的最后一列。
'您还可以将图纸名称添加到代码中,如下所示:
'工作表(“表1”)。范围(“A1:D”和最后一行(工作表(“表1”))
'使用此选项时,在运行宏时,无需工作表处于活动状态。
设置My_Range=Range(“A1:D”和LastRow(ActiveSheet))
My_Range.Parent.Select
如果ActiveWorkbook.ProtectStructure=True或_
My_Range.Parent.ProtectContents=则为True
MsgBox“抱歉,工作簿或工作表受保护时不工作”_
vbOKOnly,“复制到新工作表”
出口接头
如果结束
'更改屏幕更新、计算、启用事件。。。。
应用
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdate=False
.EnableEvents=False
以
ViewMode=ActiveWindow.View
ActiveWindow.View=xlNormalView
ActiveSheet.DisplayPageBreaks=False
'首先,卸下自动过滤器
My_Range.Parent.AutoFilterMode=False
'筛选并设置筛选字段和筛选条件:
'此示例筛选器位于范围中的第一列(如果需要,请更改字段)
'在这种情况下,范围从so字段开始,1是A列,2=B列。。。。。。
如果您想要相反的结果,请使用“荷兰”作为标准
My_Range.AutoFilter字段:=1,准则1:==荷兰
'如果要对单元格值进行筛选,可以使用此选项,使用“”作为相反的选项
'此示例使用activecell值
'My_Range.AutoFilter字段:=1,Criteria1:=“=”&ActiveCell.Value
'这将使用A2中的单元格值作为标准
'My_Range.AutoFilter字段:=1,Criteria1:=“=”&范围(“A2”).值
''如果要对Inputbox值进行筛选,请使用此
'FilterCriteria=InputBox(“您想筛选什么文本?”_
““输入筛选项。”)
'My_Range.AutoFilter字段:=1,准则1:=“=”&筛选准则
'检查是否有超过8192个区域(Excel可以复制的区域限制)
帐户=0
出错时继续下一步
CCount=My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
错误转到0
如果CCount=0,则
MsgBox“有超过8192个区域:”_
&vbNewLine&“无法复制可见数据。”_
&vbNewLine&“提示:在使用此宏之前对数据进行排序。”_
vbOKOnly,“复制到工作表”
其他的
'添加新工作表
设置WSNew=Worksheets.Add(之后:=Sheets(ActiveSheet.Index))
'询问工作表名称
sheetName=InputBox(“新工作表的名称是什么?”_
“命名新表”)
出错时继续下一步
WSNew.Name=sheetName
如果错误编号>0,则
MsgBox“更改工作表名称:&WSNew.name&_
“在宏准备就绪后手动执行。图纸名称“&”_
“您填写的内容已经存在,或者您使用了字符”&_
“工作表名称中不允许的。”
呃,明白了
如果结束
错误转到0
'将可见数据复制/粘贴到新工作表
My_Range.Parent.AutoFilter.Range.Copy
使用WSNew.Range(“A1”)
'粘贴:=8将在Excel 2000及更高版本中复制列宽
'如果使用Excel 97,请删除此行
.Paste特殊粘贴:=8
.Paste特殊XLPaste值
.Paste特殊XLPaste格式
Application.CutCopyMode=False
.选择
以
'如果要删除复制的行,也可以使用此
'使用My_Range.Parent.AutoFilter.Range
'出现错误时,请继续下一步
'设置rng=.Offset(1,0).调整大小(.Rows.Count-1、.Columns.Count)_
'.SpecialCells(xlCellTypeVisible)
'在出现错误时转到0
'如果不是,则rng为Nothing,然后rng.EntireRow.Delete
"以
如果结束
'关闭自动过滤器
My_Range.Parent.AutoFilterMode=False
'还原屏幕更新、计算、启用事件。。。。
My_Range.Parent.Select
ActiveWindow.View=ViewMode
如果不是WSNew,则WSNew为Nothing。选择
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=CalcMode
以
端接头
函数LastRow(sh作为工作表)
论错误
Sub Copy_With_AutoFilter1()
'Note: This macro use the function LastRow
    Dim My_Range As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim FilterCriteria As String
    Dim CCount As Long
    Dim WSNew As Worksheet
    Dim sheetName As String
    Dim rng As Range

    'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
    'and the header of the first column, D is the last column in the filter range.
    'You can also add the sheet name to the code like this :
    'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
    'No need that the sheet is active then when you run the macro when you use this.
    Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
    My_Range.Parent.Select

    If ActiveWorkbook.ProtectStructure = True Or _
       My_Range.Parent.ProtectContents = True Then
        MsgBox "Sorry, not working when the workbook or worksheet is protected", _
               vbOKOnly, "Copy to new worksheet"
        Exit Sub
    End If

    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False

    'Firstly, remove the AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Filter and set the filter field and the filter criteria :
    'This example filter on the first column in the range (change the field if needed)
    'In this case the range starts in A so Field 1 is column A, 2 = column B, ......
    'Use "<>Netherlands" as criteria if you want the opposite
    My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands"

    'If you want to filter on a cell value you can use this, use "<>" for the opposite
    'This example uses the activecell value
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value

    'This will use the cell value from A2 as criteria
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value

    ''If you want to filter on a Inputbox value use this
    'FilterCriteria = InputBox("What text do you want to filter on?", _
     '                              "Enter the filter item.")
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria

    'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
    CCount = 0
    On Error Resume Next
    CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
    On Error GoTo 0
    If CCount = 0 Then
        MsgBox "There are more than 8192 areas:" _
             & vbNewLine & "It is not possible to copy the visible data." _
             & vbNewLine & "Tip: Sort your data before you use this macro.", _
               vbOKOnly, "Copy to worksheet"
    Else
        'Add a new Worksheet
        Set WSNew = Worksheets.Add(After:=Sheets(ActiveSheet.Index))

        'Ask for the Worksheet name
        sheetName = InputBox("What is the name of the new worksheet?", _
                             "Name the New Sheet")

        On Error Resume Next
        WSNew.Name = sheetName
        If Err.Number > 0 Then
            MsgBox "Change the name of sheet : " & WSNew.Name & _
                 " manually after the macro is ready. The sheet name" & _
                 " you fill in already exists or you use characters" & _
                 " that are not allowed in a sheet name."
            Err.Clear
        End If
        On Error GoTo 0

        'Copy/paste the visible data to the new worksheet
        My_Range.Parent.AutoFilter.Range.Copy
        With WSNew.Range("A1")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            ' Remove this line if you use Excel 97
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With

        ' If you want to delete the rows that you copy, also use this
        ' With My_Range.Parent.AutoFilter.Range
        '     On Error Resume Next
        '     Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
        '               .SpecialCells(xlCellTypeVisible)
        '     On Error GoTo 0
        '     If Not rng Is Nothing Then rng.EntireRow.Delete
        ' End With

    End If

    'Close AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Restore ScreenUpdating, Calculation, EnableEvents, ....
    My_Range.Parent.Select
    ActiveWindow.View = ViewMode
    If Not WSNew Is Nothing Then WSNew.Select
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With

End Sub


Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlValues, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function