Excel VBA Auto Filter not sorting when false

buyers

Board Regular
Joined
Jan 7, 2016
Messages
54
For some reason the below code is filtering on all years correctly but 2020 with the below True/False argument. As you can see from the picture, it is giving a false, but not auto filtering. Any help would be great!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("F3:F5")) Is Nothing Then
    If Target.Count > 1 Then Exit Sub
    ' Change "E3" to the cell the user completes the year in
    Const tlbName = "Table3" 'update to name of table name on relevant sheet
    Dim DateCol, MinYear, MaxYear
  
    DateCol = "D9:D" & Range("D" & Rows.Count).End(xlUp).Row 'change to match your date column
    MinYear = Year(WorksheetFunction.Min(ActiveSheet.Range(DateCol)))
    MaxYear = Year(WorksheetFunction.Max(ActiveSheet.Range(DateCol)))
  
    With ActiveSheet
      If Target.Value >= MinYear And Target.Value <= MaxYear Then
        .ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1, Criteria1:="TRUE"
      Else
        .ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1, Criteria1:="TRUE"
      End If
    End With
  End If
 
End Sub
 
Sub GetListObjectNames()
  Dim ws As Worksheet
Dim lo As ListObject
  Dim rng As Range
  Set ws = ActiveSheet
  For Each lo In ws.ListObjects
    Debug.Print lo.Name
    MsgBox lo.Name
  Next lo
End Sub

image001[1].png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
No, FALSE on B9, the effective date column shows date 3/1/19, which is not between 2/1/20 and 1/1/21, all rows show false so should auto hide them. I works correctly when F3 is 2018, or 2017 vs same argument
 
Upvote 0
Click the edge of the code to add a break point.
Change F3 and the worksheet_change event will kick in stopping at that break point.
Hit F8 to step through the code

1576335093165.png
 
Upvote 0
It looks like this was resolved by deleting blank rows in the table. Thank you for your help!
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top