-1

I've attempted to implement the macro written in the answers here:

Filter a table using information in cells outside of the table

But it doesn't seem to work for dates. I've got a column set to short date format of dd/mm/yy but whenever I type anything it just hides all rows.

Edit: I've got two text columns that filter exactly as they should, it's just the date that's the issue

New contributor
Matt Murphy is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jul 4 at 15:45
  • 1
    "I've got a column set to short date format of dd/mm/yy but whenever I type anything it just hides all rows." What does that mean? Where and what are you typing? Commented Jul 4 at 16:16
  • Do you know how to debug your macro? You are going to need to determine exactly what the value is that's coming from the field where you enter the filter date value. Ultimately, Excel's dates are stored as numbers (and it's just the formatting that displays them in a friendly date format) - but if the field where you enter the date is formatted as "Text", or formatted in some other way that's not actually putting the underlying number value in the cell that Excel expects, then your comparison is not going to be working properly
    – Craig
    Commented Jul 5 at 0:52

1 Answer 1

0

To apply a filter to a date field using Date Filters > Equals..., do one of the following:

Option 1:

Apply the default Short Date format to the entire date field.

short_date_format.png

Then filter by the Value of the criteria cell. For example:

Option Explicit

Sub FilterByDateEqualsValue()
    Dim lo As ListObject, rg As Range
    Set lo = Sheet1.ListObjects("Table1")
    Set rg = Sheet1.Range("A2")
    lo.Range.AutoFilter Field:=lo.ListColumns("Date").Index, _
        Criteria1:="=" & rg.Value
End Sub

Note: you MUST concatenate the "=" sign to the criteria value with this method...

  • Criteria1:="=" & rg.Value will work
  • Criteria1:=rg.Value will NOT work

Option 2:

If a custom date format has been applied to the entire date field (i.e. dd/mm/yy), use the Format function to apply the same format to the criteria value.

Sub FilterByDateEqualsFormat()
    Dim lo As ListObject, rg As Range
    Set lo = Sheet1.ListObjects("Table1")
    Set rg = Sheet1.Range("A2")
    lo.Range.AutoFilter Field:=lo.ListColumns("Date").Index, _
        Criteria1:="=" & Format(rg.Value, "dd/mm/yy")
End Sub

Option 3:

To eliminate the potential for errors caused by incorrect or inconsistent date formatting within the date field, use the Date Filters > Between... method.

Sub FilterByDateBetween()
    Dim lo As ListObject, rg As Range
    Set lo = Sheet1.ListObjects("Table1")
    Set rg = Sheet1.Range("A2")
    lo.Range.AutoFilter Field:=lo.ListColumns("Date").Index, _
        Criteria1:=">=" & rg.Value, Operator:=xlAnd, Criteria2:="<=" & rg.Value
End Sub

If you need more assistance, please edit your question to include the code you are currently using.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .