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. Below is my current code. When I run this code nothing happens so I believe I have my If and When Loops setup incorrectly.
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