Excel VBA - Date Filter AND blanks

wongjoh1

New Member
Joined
Feb 3, 2016
Messages
3
I would like to filter data between two dates AND blank. I could do the part for filter data between two dates, but I have trouble with filtering the blank as well. My code is as follows, please help,

Sub Filter()

Dim lngStart As Long, lngEnd As Long

Sheets(“Sheet1”).Select
lngStart = Range(“A2”).Value ‘this is the start date
lngEnd = Range(“B2”).Value + 1 ‘this is the end date

Sheets(“Sheet2”).Select
Selection.AutoFilter Field:=1, _
Criteria1:=“>=” & lngStart, _
Operator:=xlAnd, _
Criteria2:=“<=” & lngEnd

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this

Code:
[B]Sub Filter_Dates_and_Blanks()[/B]
  Dim sh2 As Worksheet, dict As Object, c As Range
  Set sh2 = Sheets("[COLOR=#0000ff]Sheet2[/COLOR]")
  Set dict = CreateObject("Scripting.Dictionary")
  For Each c In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(xlUp))
    If c >= Sheets("[COLOR=#0000ff]Sheet1[/COLOR]").Range("A2") And c <= Sheets("[COLOR=#0000ff]Sheet1[/COLOR]").Range("B2") Then
      dict.Item(c.Row & "A") = 2
      dict.Item(c.Row & "B") = Format(c.Value, "mm/dd/yyyy")
    End If
  Next
[COLOR=#008000]  sh2.Range("A1").AutoFilter 1, Array("="), xlFilterValues, dict.items[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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