Excel Autofilter doesn't work on column with date and time with VBA code

Bira

I'm having trouble when filtering date and time values together using VBA on MS Excel. When I need to filter only dates, I find no problem using the code below:

myRange.AutoFilter Field:=3, Criteria1:= _
    ">=" & CDbl(CDate((Date - 2))), Operator:=xlAnd, Criteria2:="<=" & CDbl(CDate((Date)))

The question is: how could I specify a time when filtering? Example: I'd like to filter results from yesterday at 19:00 until today at 8:00.

Bira

I found out a way:

Sub DateTimeFilter()

    Dim d1, d2, m1, m2, y1, y2 As Integer

    Dim dt1, dt2 As String

    d1 = Day(Date - 1)
    d2 = Day(Date)
    m1 = Month(Date - 1)
    m2 = Month(Date)
    y1 = Year(Date - 1)
    y2 = Year(Date)

    dt1 = m1 & "/" & d1 & "/" & y1
    dt2 = m2 & "/" & d2 & "/" & y2

    ActiveSheet.Range("$A$3:$L$2012").AutoFilter Field:=3, Criteria1:= _
        ">=" & dt1 & " 19:00", Operator:=xlAnd, Criteria2:="<=" & dt2 & " 08:00"

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel VBA add Autofilter if it doesn't exist

From Dev

Application.DisplayAlerts = True doesn't work in vba excel code

From Dev

VBA Code doesn't work on all excel sheets

From Dev

For Each doesn't work in excel vba

From Dev

Automatic date and time doesn't work

From Dev

Set AutoFilter in Excel with VBA

From Dev

Excel VBA AutoFilter Codes

From Dev

Excel VBA AutoFilter Percentages

From Dev

SQL code won't work in Excel VBA

From Dev

Autofilter with column formatted as date

From Dev

VBA Autofilter Set by Date in Excel - not showing any data

From Dev

VBA Excel autofilter for any date before the current month

From Dev

VBA Autofilter Set by Date in Excel - not showing any data

From Dev

Excel VBA Autofilter issues with multiple inputs per column

From Dev

Setting a range based on values (why doesn't this code work?) with VBA/excel

From Dev

boost Date_Time date parsing doesn't work

From Dev

Excel VBA autofilter all but three

From Dev

Autofilter on Mutliple Columns Excel VBA

From Dev

vba: Offset doesn't work from Word script in Excel doc

From Dev

VBA excel row copying method doesn't work

From Dev

Delete all duplicate rows Excel vba doesn´t work correctly

From Dev

Excel VBA: Easy If function doesn't seem to work properly

From Dev

vba: Offset doesn't work from Word script in Excel doc

From Dev

Excel VBA: Easy If function doesn't seem to work properly

From Dev

VBA for Excel, activecell doesn't work while writing function

From Dev

Why Select Case doesn't work in this code VBA?

From Dev

Excel Macro: (1) Code below doesn't work (2) How do i make a macro run real time on any sheet

From Dev

Why doesn't my date validation code work?

From Dev

$.get doesn’t work until the code is executed another time

Related Related

  1. 1

    Excel VBA add Autofilter if it doesn't exist

  2. 2

    Application.DisplayAlerts = True doesn't work in vba excel code

  3. 3

    VBA Code doesn't work on all excel sheets

  4. 4

    For Each doesn't work in excel vba

  5. 5

    Automatic date and time doesn't work

  6. 6

    Set AutoFilter in Excel with VBA

  7. 7

    Excel VBA AutoFilter Codes

  8. 8

    Excel VBA AutoFilter Percentages

  9. 9

    SQL code won't work in Excel VBA

  10. 10

    Autofilter with column formatted as date

  11. 11

    VBA Autofilter Set by Date in Excel - not showing any data

  12. 12

    VBA Excel autofilter for any date before the current month

  13. 13

    VBA Autofilter Set by Date in Excel - not showing any data

  14. 14

    Excel VBA Autofilter issues with multiple inputs per column

  15. 15

    Setting a range based on values (why doesn't this code work?) with VBA/excel

  16. 16

    boost Date_Time date parsing doesn't work

  17. 17

    Excel VBA autofilter all but three

  18. 18

    Autofilter on Mutliple Columns Excel VBA

  19. 19

    vba: Offset doesn't work from Word script in Excel doc

  20. 20

    VBA excel row copying method doesn't work

  21. 21

    Delete all duplicate rows Excel vba doesn´t work correctly

  22. 22

    Excel VBA: Easy If function doesn't seem to work properly

  23. 23

    vba: Offset doesn't work from Word script in Excel doc

  24. 24

    Excel VBA: Easy If function doesn't seem to work properly

  25. 25

    VBA for Excel, activecell doesn't work while writing function

  26. 26

    Why Select Case doesn't work in this code VBA?

  27. 27

    Excel Macro: (1) Code below doesn't work (2) How do i make a macro run real time on any sheet

  28. 28

    Why doesn't my date validation code work?

  29. 29

    $.get doesn’t work until the code is executed another time

HotTag

Archive