Filtering Date based on cell value in Excel VBA

primasatria

New Member
Joined
Dec 3, 2023
Messages
11
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Dear all,

I am using MS Office 365, I need to filter based on multiple criteria and based on date based on cell value in cell K1.
The problem, I tried by to run this code below, but somehow I can not filtered date in column K, while others can be filtered as I wanted.


VBA Code:
Sub Filter_DailyReport()
    Dim fdate As Long
    Range("AK1").FormulaR1C1 = "=TODAY()-1"
    Range("AK1").Value = Range("AK1").Value
    'Range("AK1").NumberFormat = "[$-en-US]dd-mmm-yy;@"
    fdate = Range("AK1").Value
    
    Set ws = ThisWorkbook.Sheets("Database")
    ws.ListObjects("Table1").Range.AutoFilter Field:=28, Criteria1:="Approved"
    ws.ListObjects("Table1").Range.AutoFilter Field:=30, Criteria1:="Red"
    ws.ListObjects("Table1").Range.AutoFilter Field:=37, Criteria1:=fdate
    ws.ListObjects("Table1").Range.AutoFilter Field:=38, Criteria1:="FE Const."
    ws.ListObjects("Table1").Range.AutoFilter Field:=41, Criteria1:="JPK Utara"

    ' Define the range to apply the filter to
    'Set rng = ws.Range("G2:G" & ws.Cells(ws.Rows.Count, "G").End(xlUp).Row)

    ' Clear any existing filters
    'ws.AutoFilterMode = False
End Sub

Is there anybody could helping me? I've been stuck with this code for 3 days already.

Thank you so much, for your helping hand.

Best regards,
Prima - Indonesia
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try Dim fdate As Date. Date values in US format are floating point doubles, not longs. I don't see the point of this line:
Range("AK1").Value = Range("AK1").Value To me, that's like saying 10 = 10.
I suspect you're not using US date format, which is what Excel is based on regardless of your regional settings (as far as I know). I don't know if the NumberFormat property will work for you or not as I'm not familiar with it. If you do as suggested, make sure you test with ambiguous dates as well. An example of that is 02/11/2024. To me, that is February 11. To others, it is November 2nd.

Also, this is a way to concatenate date values as US format in Access so maybe it would work for Excel also:
Format(fdate, "\#mm\/dd\/yyyy") & "#;"
You might need to declare fdate as a Variant to use that.
 
Upvote 0
Solution
Try Dim fdate As Date. Date values in US format are floating point doubles, not longs. I don't see the point of this line:
Range("AK1").Value = Range("AK1").Value To me, that's like saying 10 = 10.
I suspect you're not using US date format, which is what Excel is based on regardless of your regional settings (as far as I know). I don't know if the NumberFormat property will work for you or not as I'm not familiar with it. If you do as suggested, make sure you test with ambiguous dates as well. An example of that is 02/11/2024. To me, that is February 11. To others, it is November 2nd.

Also, this is a way to concatenate date values as US format in Access so maybe it would work for Excel also:
Format(fdate, "\#mm\/dd\/yyyy") & "#;"
You might need to declare fdate as a Variant to use that.
Dear Micron,

Thanks for helping me.
Finally, based on your suggestion I changed fdate as Variant as below.
Tested several times with different date and it works.
Problem solved!


VBA Code:
Sub Filter_DailyReport()
    'Dim fdate As Date
    Dim fdate As Variant
    Range("AK1").FormulaR1C1 = "=TODAY()-1"
    Range("AK1").Value = Range("AK1").Value
    fdate = Range("AK1").Value
    
    Set ws = ThisWorkbook.Sheets("Database")
    ws.ListObjects("Table1").Range.AutoFilter Field:=28, Criteria1:="Approved"
    ws.ListObjects("Table1").Range.AutoFilter Field:=30, Criteria1:="Red"
    'ws.ListObjects("Table1").Range.AutoFilter Field:=37, Criteria1:=fdate
    ws.ListObjects("Table1").Range.AutoFilter Field:=37, Operator:= _
        xlFilterValues, Criteria2:=Array(2, fdate)
    ws.ListObjects("Table1").Range.AutoFilter Field:=38, Criteria1:="FE Const."
    ws.ListObjects("Table1").Range.AutoFilter Field:=41, Criteria1:="JPK Utara"


    ' Clear any existing filters
    'ws.AutoFilterMode = False
End Sub


Thank you very much.
Really appreciate it.

Dear MrExcel,
Thank you very much

Regards,
prima - Indonesia
 
Upvote 0

Forum statistics

Threads
1,225,730
Messages
6,186,701
Members
453,369
Latest member
positivemind

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