I know I am a complete noob. But is there a better way to run the below code using loops? Please guide me. I have am trying to use autofilter conditions on a range B14:P14 and a big data set. I want the autofilters to trigger is someone makes any changes to the range B14: P14. Its a code to sort out available hours for a list of employees weekly. Your help will be greatly appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("B14") Then
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=14, Criteria1:=">=" & Range("B14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=15, Criteria1:=">=" & Range("C14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=16, Criteria1:=">=" & Range("D14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=17, Criteria1:=">=" & Range("E14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=18, Criteria1:=">=" & Range("F14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=19, Criteria1:=">=" & Range("G14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=20, Criteria1:=">=" & Range("H14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=21, Criteria1:=">=" & Range("I14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=22, Criteria1:=">=" & Range("J14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=23, Criteria1:=">=" & Range("K14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=24, Criteria1:=">=" & Range("L14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=25, Criteria1:=">=" & Range("M14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=26, Criteria1:=">=" & Range("N14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=27, Criteria1:=">=" & Range("O14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=28, Criteria1:=">=" & Range("P14").Text
Else
If Target = Range("C14") Then
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=14, Criteria1:=">=" & Range("B14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=15, Criteria1:=">=" & Range("C14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=16, Criteria1:=">=" & Range("D14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=17, Criteria1:=">=" & Range("E14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=18, Criteria1:=">=" & Range("F14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=19, Criteria1:=">=" & Range("G14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=20, Criteria1:=">=" & Range("H14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=21, Criteria1:=">=" & Range("I14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=22, Criteria1:=">=" & Range("J14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=23, Criteria1:=">=" & Range("K14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=24, Criteria1:=">=" & Range("L14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=25, Criteria1:=">=" & Range("M14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=26, Criteria1:=">=" & Range("N14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=27, Criteria1:=">=" & Range("O14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=28, Criteria1:=">=" & Range("P14").Text
Else
If Target = Range("D14") Then
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=14, Criteria1:=">=" & Range("B14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=15, Criteria1:=">=" & Range("C14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=16, Criteria1:=">=" & Range("D14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=17, Criteria1:=">=" & Range("E14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=18, Criteria1:=">=" & Range("F14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=19, Criteria1:=">=" & Range("G14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=20, Criteria1:=">=" & Range("H14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=21, Criteria1:=">=" & Range("I14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=22, Criteria1:=">=" & Range("J14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=23, Criteria1:=">=" & Range("K14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=24, Criteria1:=">=" & Range("L14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=25, Criteria1:=">=" & Range("M14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=26, Criteria1:=">=" & Range("N14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=27, Criteria1:=">=" & Range("O14").Text
Range(ActiveSheet.Cells(19, 1), ActiveSheet.Cells(19, 28)).AutoFilter field:=28, Criteria1:=">=" & Range("P14").Text
End if
End if
End if
End Sub
I modify your first if so you can see an example
Hope it Helps!