简体   繁体   English

VBA 高级过滤项目

[英]VBA Advanced Filtering Project

I am working on a project that will filter a data table by a given criteria, copy the filtered data, paste that data into a new sheet, then delete that data from the data table.我正在开发一个项目,该项目将按给定条件过滤数据表,复制过滤后的数据,将该数据粘贴到新工作表中,然后从数据表中删除该数据。 This code worked great when I only had one set of criteria to filter by, however, there are 8 total criteria sets I need the dataset to be filtered by separately.当我只有一组标准可供过滤时,此代码效果很好,但是,总共有 8 个标准集,我需要单独过滤数据集。 Below is my current code.以下是我当前的代码。 When I run this code nothing happens so I believe I have my If and When Loops setup incorrectly.当我运行这段代码时,什么都没有发生,所以我相信我的 If 和 When 循环设置不正确。

Any help would be appreciated.任何帮助,将不胜感激。 Please let me know if more explanation is needed.如果需要更多解释,请告诉我。

Sub Test_Filter()
    
    Dim Source      As Range        ' Data to look at
    Dim Data        As Range        ' Filtered data to copy
    Dim criteria    As Range        ' Criteria for Advanced Filter
    Dim Destination As Range        ' Place to copy filtered data
    Dim Area        As Range
    Dim RC          As Worksheet
    '-----------------------------------------------
    
    Dim RL          As Variant
    'Dimension the count of all flight exception buckets
    Dim HotLI       As Variant
    Dim HotM        As Variant
    Dim HotW        As Variant
    Dim HotC        As Variant
    
    Dim ColdLI      As Variant
    Dim COldM       As Variant
    Dim ColdW       As Variant
    Dim ColdC       As Variant
    
    HotLI = Sheets("OPC Exception").Range("W18").Value
    HotM = Sheets("OPC Exception").Range("Z18").Value
    HotW = Sheets("OPC Exception").Range("AC18").Value
    HotC = Sheets("OPC Exception").Range("AF18").Value
    
    ColdLI = Sheets("OPC Exception").Range("W57").Value
    COldM = Sheets("OPC Exception").Range("Z57").Value
    ColdW = Sheets("OPC Exception").Range("AC57").Value
    ColdC = Sheets("OPC Exception").Range("AF57").Value
    '--------------------------------------------------------------------------
    'Defining values for Non Empty If statement
    
    NonEmpty_HotLI = Sheets("OPC Exception").Range("V18").Value
    NonEmpty_HotM = Sheets("OPC Exception").Range("Y18").Value
    NonEmpty_Hotw = Sheets("OPC Exception").Range("AB18").Value
    NonEmpty_HotC = Sheets("OPC Exception").Range("AE18").Value
    
    NonEmpty_ColdLI = Sheets("OPC Exception").Range("V57").Value
    NonEmpty_ColdM = Sheets("OPC Exception").Range("Y57").Value
    NonEmpty_ColdW = Sheets("OPC Exception").Range("AB57").Value
    NonEmpty_ColdC = Sheets("OPC Exception").Range("AE57").Value
    
    Set Source = Sheets("Working").Range("A1").CurrentRegion
    '--------------------------------------------------------------------------------
    'set criteria for each filter
    Set criteria_HotLI = Sheets("OPC Exception").Range("V20:W" & HotLI)
    Set criteria_HotM = Sheets("OPC Exception").Range("Y20:W" & HotM)
    Set criteria_HotW = Sheets("OPC Exception").Range("AB20:W" & HotW)
    Set criteria_HotC = Sheets("OPC Exception").Range("AE20:W" & HotC)
    
    Set criteria_ColdLI = Sheets("OPC Exception").Range("V59:V" & ColdLI)
    Set criteria_ColdM = Sheets("OPC Exception").Range("Y59:V" & COldM)
    Set criteria_ColdW = Sheets("OPC Exception").Range("AB59:V" & ColdW)
    Set criteria_ColdC = Sheets("OPC Exception").Range("AE59:V" & ColdC)
    
    '--------------------------------------------------------------------------------
    'set destination for each bucket
    
    Set Destination_Int = Sheets("International").Range("A1")
    Set Destination_Weather = Sheets("Weather").Range("A1")
    Set Destination_Mech = Sheets("Mech").Range("A1")
    Set Destination_Covid = Sheets("Covid").Range("A1")
    Set Destination_LA = Sheets("Late Air").Range("A1")
    Set Destination_K9 = Sheets("K9").Range("A1")
    Set Destination_LT = Sheets("Late Trailer").Range("A1")
    Set Destination_Cap = Sheets("Capacity").Range("A1")
    Set Destination_MF = Sheets("Misflow").Range("A1")
    Set Destination_LIB = Sheets("LIB").Range("A1")
    
    '--------------------------------------------------------------------------------
    'Start filtering Working into buckets based on If nonempty statement
    With Source
    
    If NonEmpty_HotLI = "False" Then
        
        GoTo JumpHotM
        
    ElseIf NonEmpty_HotLI = "TRUE" Then
        
      
            .AdvancedFilter xlFilterInPlace, criteria_HotLI
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then
                GoTo JumpHotM
            End If
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_LA.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
        
    End If
    
JumpHotM:
    
    If NonEmpty_HotM = "False" Then
        
        GoTo JumpHotW
        
    ElseIf NonEmpty_HotM = "True" Then
       
            .AdvancedFilter xlFilterInPlace, criteria_HotM
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then GoTo JumpHotW
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_Mech.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
     
    End If
    
JumpHotW:
    If NonEmpty_Hotw = "False" Then
        
        GoTo JumpHotC
        
    ElseIf NonEmpty_Hotw = "True" Then
        
            .AdvancedFilter xlFilterInPlace, criteria_HotW
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then GoTo JumpHotC
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_Weather.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
        
    End If
    
JumpHotC:
    If NonEmpty_HotC = "FALSE" Then
        
        GoTo JumpColdLI
        
    ElseIf NonEmpty_HotC = "True" Then
        
            .AdvancedFilter xlFilterInPlace, criteria_HotC
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then GoTo JumpColdLI
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_Covid.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
      
    End If
    
JumpColdLI:
    If NonEmpty_ColdLI = "False" Then
        
        GoTo JumpColdM
        
    ElseIf NonEmpty_ColdLI = "False" Then
        
      
            .AdvancedFilter xlFilterInPlace, criteria_ColdLI
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then GoTo JumpColdM
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_LA.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
     
    End If
    
JumpColdM:
    
    If NonEmpty_ColdM = "False" Then
        
        GoTo JumpColdW
        
    ElseIf NonEmpty_ColdM = "False" Then
        
            .AdvancedFilter xlFilterInPlace, criteria_ColdM
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then GoTo JumpColdW
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_Mech.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
       
    End If
    
JumpColdW:
    If NonEmpty_ColdW = "False" Then
        
        GoTo JumpColdC
        
    ElseIf NonEmpty_ColdW = "True" Then
       
            .AdvancedFilter xlFilterInPlace, criteria_ColdW
            
            Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
            .Parent.ShowAllData
            
            If Data Is Nothing Then GoTo JumpColdC
            
            For Each Area In Data.Areas
                Area.Copy
                Destination_Weather.Insert xlShiftDown
            Next Area
            Data.Delete xlShiftUp
       
    End If
    
JumpColdC:
    If NonEmpty_ColdC = "False" Then
        
        GoTo JumpEnd
        
    ElseIf NonEmpty_ColdC = "True" Then
       
        
        .AdvancedFilter xlFilterInPlace, criteria_ColdC
        
        Set Data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
        .Parent.ShowAllData
        
        If Data Is Nothing Then GoTo JumpEnd
        
        For Each Area In Data.Areas
            Area.Copy
            Destination_Covid.Insert xlShiftDown
        Next Area
        Data.Delete xlShiftUp
   
    End If
    
    End With


JumpEnd:

End Sub

The sub below is based on looking at your code and use 4 variables from you code just for example.下面的子代码基于查看您的代码并使用代码中的 4 个变量,例如。 Not tested in my side .在我这边没有测试

Basically, the sub do the filtering only when it meet a condition where any of your NonEmpty_ variable value is a text "True" and if the Data variable is not nothing then it do the loop for inserting and deleting.基本上,子程序仅在满足您的任何 NonEmpty_ 变量值为文本“True”的条件时才进行过滤,并且如果 Data 变量不是空的,则它会执行插入和删除的循环。

Sub test()
Dim criteria_HotLI As Range: Dim Destination_Int As Range
Dim criteria_HotM As Range: Destination_Weather As Range
Dim criteria_HotW As Range: Dim Destination_Mech As Range
Dim criteria_HotC As Range: Destination_Covid As Range
Dim arr As Variant: Dim i As Long: Dim data As Range: Dim area As Long

With Sheets("OPC Exception")
Set criteria_HotLI = .Range("V20:W" & .Range("W18").Value)
Set criteria_HotM = .Range("Y20:W" & .Range("Z18").Value)
Set criteria_HotW = .Range("AB20:W" & .Range("AC18").Value)
Set criteria_HotC = .Range("AE20:W" & .Range("AF18").Value)
arr = Array(.Range("V18").Value, .Range("Y18").Value, .Range("AB18").Value, .Range("AE18").Value)

'or for the array using variable
'NonEmpty_HotLI = .Range("V18").Value
'NonEmpty_HotM = .Range("Y18").Value
'NonEmpty_Hotw = .Range("AB18").Value
'NonEmpty_HotC = .Range("AE18").Value
'arr = Array(NonEmpty_HotLI, NonEmpty_HotM, NonEmpty_Hotw, NonEmpty_HotC)

End With

    Set Destination_Int = Sheets("International").Range("A1")
    Set Destination_Weather = Sheets("Weather").Range("A1")
    Set Destination_Mech = Sheets("Mech").Range("A1")
    Set Destination_Covid = Sheets("Covid").Range("A1")

For i = LBound(arr) To UBound(arr)
    If arr(i) = "True" Then

        Select Case i + 1
            Case 1: Set crit = criteria_HotLI: Set dest = Destination_LA 'if NonEmpty_HotLI = "True"
            Case 2: Set crit = criteria_HotM: Set dest = Destination_Mech 'if NonEmpty_HotM = "True"
            Case 3: Set crit = criteria_HotW: Set dest = Destination_Weather 'if NonEmpty_Hotw = "True"
            Case 4: Set crit = criteria_HotC: Set dest = Destination_Covid 'if NonEmpty_HotC = "True"
        End Select
        
        With Sheets("OPC Exception") ' your code with Source ??
        .AdvancedFilter xlFilterInPlace, crit
        Set data = .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible)
        .Parent.ShowAllData
            If Not data Is Nothing Then
                For Each area In data.Areas
                    area.Copy
                    dest.Insert xlShiftDown
                Next area
                data.Delete xlShiftUp
            End If
        End With

    End If
Next i

End Sub

The sub doesn't create the NonEmpty_ variables, but directly put the each NonEmpty_ variable value into an array. sub 不创建 NonEmpty_ 变量,而是直接将每个 NonEmpty_ 变量值放入数组中。 Since I only use 4 condition from your code, then the array will have 4 elements where the value is either a text "True" or "False".由于我只使用代码中的 4 个条件,因此该数组将有 4 个元素,其中值是文本“True”或“False”。

Then it loop to each element in the array as i.然后它以 i 的形式循环到数组中的每个元素。
Check if the arr(i) value = "True", then it give a value to a variable crit and variable dest regarding the range needed.检查 arr(i) 值是否 = "True",然后根据所需范围为变量 crit 和变量 dest 赋值。

Then it filter the sheet "OPC Exception" with crit variable value (actually I don't know the name of the sheet, because in your code, you define it as Source variable. So please change the sheet name if your Source variable is not "OPC Exception").然后它使用 crit 变量值过滤工作表“OPC Exception”(实际上我不知道工作表的名称,因为在您的代码中,您将其定义为 Source 变量。所以如果您的 Source 变量不是,请更改工作表名称“OPC 异常”)。

Then it set the visible range as data variable, check if data is not nothing then it do the inserting to the dest variable value and do the deleting.然后将可见范围设置为数据变量,检查数据是否为空,然后将其插入到目标变量值并进行删除。

Please put attention to the order of the array, because the crit and dest variable depends on the order of the array.请注意数组的顺序,因为 crit 和 dest 变量取决于数组的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM