VBA Autofilter between date range and include blanks

choulee

New Member
Joined
Aug 16, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a date column at column 16. I want to filter between July 1, 2022 and July 31, 2022 and include all the blanks, as well. My code lines below didn't seem to work for me. If you can help, I will be very thankful.

Sub function1()

startdate = #7/1/2022#
enddate = #7/31/2022#

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Test")

sh.UsedRange.AutoFilter 16, "=", xlOr, "<=" & enddate
sh.UsedRange.AutoFilter 16, ">=" & startdate, xlOr, "="

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Dates are notoriously tricky with Autofilter. See if the following gives you what you want.

VBA Code:
Option Explicit
Sub choulee()
    Dim sh As Worksheet
    Set sh = Worksheets("Test")
    Dim startdate As Date
    Dim enddate As Date
    
    startdate = Format("7/1/2022", "m/d/yyyy")
    enddate = Format("7/31/2022", "m/d/yyyy")
    
    With sh.UsedRange
        .AutoFilter 16, ">=" & CLng(startdate), 1, "<=" & CLng(enddate)
    End With

End Sub
 
Upvote 0
This second method should pick up blank cells as well.

VBA Code:
Option Explicit
Sub choulee_2()
    Dim lRow As Long, i As Long, c As Range, arr() As Variant
    Dim sh As Worksheet
    Set sh = Worksheets("Test")
    
    Dim startdate As Date
    Dim enddate As Date
    startdate = Format("7/1/2022", "m/d/yyyy")
    enddate = Format("7/31/2022", "m/d/yyyy")

    lRow = Cells.Find("*", , xlFormulas, , 1, 2).Row
    If lRow = 1 Then lRow = 2
    For Each c In sh.Range("P2:P" & lRow)
        If c = "" Or (CLng(c) >= CLng(startdate) And CLng(c) <= CLng(enddate)) Then
            ReDim Preserve arr(i)
            arr(i) = Format(c, "m/d/yyyy")
            i = i + 1
        End If
    Next c

    With sh.Range("A1").CurrentRegion
        .AutoFilter 16, Array(arr), 7
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

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