Consulting

Results 1 to 3 of 3

Thread: Date filter in table

  1. #1
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    Date filter in table

    Hi all,

    I have a table of staff names with, where applicable, employment end dates.
    I wish to filter the table to show all staff relevant to the current financial year
    This requires that I select dates in the current fin year as well as fields with no date

    my current code results in no records being selected. can anyone see my error?

    thanks
    Werafa

    Private Sub tglHideYears_Click()
    Dim mysheet As Worksheet
    Dim myTable As ListObject
    Dim filterDate As String
    
    Set mysheet = ThisWorkbook.Worksheets("Staff List")
    Set myTable = mysheet.ListObjects("tblStaff")
    
        filterDate = Now()
        If Month(filterDate) > 6 Then
            filterDate = "30/6/" & Year(filterDate)
        Else
            filterDate = "30/6/" & Year(filterDate) - 1
        End If
        
    
        If tglHideYears = True Then
            'the problem code
            myTable.Range.AutoFilter Field:=4, Criteria1:="> 30/6/2018", Operator:=xlAnd, Criteria2:=""""""
            tglHideYears.Caption = "Show All Years"
        End If
        If tglHideYears = False Then
            myTable.Range.AutoFilter Field:=4
            tglHideYears.Caption = "Show Current Year"
        End If
        
    End Sub
    update:
    myTable.Range.AutoFilter Field:=4, Criteria1:="> 30/6/2018", Operator:=xlOr, Criteria2:="="
    causes the 'no employment end dates' to show correctly, but I still do not have the row with EED = 28/11/2018

    Thanks
    Last edited by werafa; 05-02-2019 at 05:10 PM.
    Remember: it is the second mouse that gets the cheese.....

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Try:
    Criteria1:=">" & Format(filterDate, "mm/dd/yyyy"), Operator:=xlOr, Criteria2:="="
    Think i have come across this issue before, autofilter within VBA only seems to work with the American date format "mm/dd/yyyy"
    The above just forces the filterDate to be in the American format, this solved my issue in the past.

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    Thanks georgiboy,

    this did work. For reference to anyone else reading this, what I did not glean from other pages is that the format in the vba code does not have to match the worksheet formatting.


    the final full line of code is:
    myTable.Range.AutoFilter Field:=4, Criteria1:=">" & Format(filterDate, "mm/dd/yyyy"), _
                Operator:=xlOr, Criteria2:="="
    this gives me the filter by date as well as leaving any row with no entry

    Thanks
    Werafa
    Remember: it is the second mouse that gets the cheese.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •