Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A2:A9999")) Is Nothing Then
With Target(1, 9)
.Value = Date
End With
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngF As Range
Dim rngFS As Range
Dim lRow As Long
Dim lCol As Long
Set rngF = ActiveSheet.AutoFilter.Range
Set rngFS = ActiveSheet.Range("FilterStatus")
lCol = rngF.Columns(1).Column - 1
lRow = rngF.Columns(1).Row
' -------------------------------------------------------------------
If Target.Count > 1 Then GoTo exitHandler
' -------------------------------------------------------------------
If Target.Address = rngFS.Address Then
If rngFS.Value = "On" Then
rngFS.Value = "Off"
Else
rngFS.Value = "On"
End If
End If
If UCase(rngFS.Value) = "ON" Then
If Not Intersect(Target, rngF) Is Nothing Then
If Target.Row > lRow Then
rngF.AutoFilter Field:=Target.Column - lCol, _
Criteria1:=Target.Value
ElseIf Target.Row = lRow Then
rngF.AutoFilter Field:=Target.Column - lCol
End If
End If
End If
exitHandler:
Exit Sub
End Sub
'Two event macros to trap our conditioners, pass the Target as a variable
Private Sub Worksheet_Change(ByVal Target As Range)
Call CombineMacro(Target)
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call CombineMacro(Target)
End Sub
'=============================
Private Sub CombineMacro(myTarget As Range)
'This is where we do all of the actual work
Dim rngF As Range
Dim rngFS As Range
Dim lRow As Long
Dim lCol As Long
Set rngF = ActiveSheet.AutoFilter.Range
Set rngFS = ActiveSheet.Range("FilterStatus")
lCol = rngF.Columns(1).Column - 1
lRow = rngF.Columns(1).Row
If myTarget.Cells.Count > 1 Then Exit Sub
'Should turn this on since you're about to make a change.
'Prevents recursive calling of Change Event macros
Application.EnableEvents = False
If Not Intersect(myTarget, Range("A2:A9999")) Is Nothing Then
myTarget(1, 9).Value = Date
End If
If myTarget.Address = rngFS.Address Then
If rngFS.Value = "On" Then
rngFS.Value = "Off"
Else
rngFS.Value = "On"
End If
End If
If UCase(rngFS.Value) = "ON" Then
If Not Intersect(myTarget, rngF) Is Nothing Then
If myTarget.Row > lRow Then
rngF.AutoFilter Field:=myTarget.Column - lCol, _
Criteria1:=myTarget.Value
ElseIf myTarget.Row = lRow Then
rngF.AutoFilter Field:=myTarget.Column - lCol
End If
End If
End If
Application.EnableEvents = True
exitHandler:
End Sub
Just to remind you, that the above code will only work if you've pasted the entire code in the respective sheet module. It is an event triggered procedure that is based on the class within which it is written (read supposed to be written), in your case, the sheet module.