Question about filtering a table from a query through VBA - Dates

Exceluser111

New Member
Joined
Jan 14, 2015
Messages
7
I have the following macro:
VBA Code:
Sub FilterByDate()

    Dim targetDate As Date
    Dim wsDashboard As Worksheet
    Dim wsShippingQ As Worksheet
    Dim tbl As ListObject
    Dim colNum As Integer

    'Define the worksheets
    Set wsDashboard = ThisWorkbook.Worksheets("Dashboard_Today")
    Set wsShippingQ = ThisWorkbook.Worksheets("ShippingQ")

    'Get the target date from the Dashboard_Today worksheet
    targetDate = wsDashboard.Range("D1").value

    'set table to the tbl variable
    Set tbl = wsShippingQ.ListObjects(1)

    'Find the Shipment_Date column number
    colNum = tbl.ListColumns("Shipment_Date").Index

    'Filter the table based on the Shipment_Date column
    tbl.Range.AutoFilter Field:=colNum, Criteria1:=targetDate, Operator:=xlFilterValues

End Sub

However, it is filtering the entire table away as if there was no match, despite the value in D1 being a date in the same format as the dates listed in the column "Shipment_Date"

I don't know if there is specific ways to interact with tables created from queries, but I am not having any success with filtering the table by the date I want.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
In classic fashion upon posting I figure out a solution that really I should have thought about.

When I recorded a macro of me manually doing the same filtering it came as a text in the output.

VBA Code:
targetDateString = Format(wsDashboard.Range("D1").value, "m/d/yy")

Did the trick
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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