Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Filter on From and To Dates not working

0

Hi all, this one has got me stumped!

I am writing an Excel VBA Application for a Catering Company who run Functions. I am trying to Filter my Database on a From and To Date for Functions booked between two dates. I am also Sorting the Database on whether it is Proceeding (YES or NO in Column 10) and Function Date Order (in Column 2) in ascending order.

PROBLEM
Not all Functions are appearing between the two dates, only some?

Dates are formatted as Custom > "dd/mm/yyyy" both on the Input Worksheet (Cells AV23 and BA23), which hold the From and To Dates, and the Database Column 2 that holds each Function Date.

HERE IS MY CODE

'-----------------------------------------------------------'
' LIST ALL FUNCTIONS IN LISTBOX1 THAT MATCH FROM & TO DATES '
' CALLED FROM:                                              '
' - Sub cmdFilter_Click()                                   '
'-----------------------------------------------------------'
' 
Private Sub FilterFunction()

     Dim fromDate, toDate As Date

     If wsInput.Range("AV23") = "" Then ' If From is Blank
          MsgBox "FROM Date needs to be input."
          Exit Sub
     End If

     If wsInput.Range("BA23") = "" Then ' If To is Blank
          MsgBox "TO Date needs to be input."
          Exit Sub
     End If

     ' Populate Date Range
     fromDate = wsInput.Range("AV23").Value
     toDate = wsInput.Range("BA23").Value

     Application.ScreenUpdating = False

     ' Filter Database on Functions Proceeding in Selected Date Range
     With db
          .Visible = True
          .ListObjects("tblDb").Range.AutoFilter Field:=10, Criteria1:="YES"
          .ListObjects("tblDb").Range.AutoFilter Field:=2, Criteria1:=">=" & CDbl(fromDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(toDate)

          ' Sort Database on Function Date and Main Meal Service Time following Filtering
          Call SortDatabase
          .Activate
          .Range("A1").Select
     End With

     Application.ScreenUpdating = True

End Sub

'--------------------------------'
' SORT DATABASE ON DATE AND TIME '
' CALLED FROM:                   '
' - Sub FilterFunction()         '
'--------------------------------'
' 
Sub SortDatabase()

    ActiveWorkbook.Worksheets("Database").ListObjects("tblDb").Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Database").ListObjects("tblDb").Sort.SortFields. _
        Add2 Key:=Range("tblDb[Function Date]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Database").ListObjects("tblDb").Sort.SortFields. _
        Add2 Key:=Range("tblDb[Main Meal Service Time]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("Database").ListObjects("tblDb").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

Can someone please help me?

Answer
Discuss

Answers

0
Selected Answer

Hi Tony.

Rushed answer but hope it helps:

Fisrtly declare both variables fully be replacing:

Dim fromDate, toDate As Date

(which makes fromDate a Variant) with:

Dim fromDate as Date, toDate As Date

Then in your filter line, don't convert it to a Double number, i.e. remove the CDbl() parts to make it:

.ListObjects("tblDb").Range.AutoFilter Field:=2, Criteria1:=">=" & fromDate, Operator:=xlAnd, Criteria2:="<=" & toDate

Also you might get ideas from Don's excellent tutorial Elegant Date and Location Filtering in Excel - A Must See!

Hope this fixes things (I don't have your file or time to test at present).

Discuss

Discussion

Thanks John, but your solution still didn't work.

I will try the "Slicer" solution in Don's Video.
tonyimbruglia (rep: 6) Dec 14, '22 at 7:17 pm
Don's Slicer Video is brilliant! Something I never new. and I was able to put the Slicers on a different Worksheet to the the one being filtered, which was exactly wahat I wanted.

Thank you John for recommending it, and thank you Don for your insight.

I would recommend that everyone watch this video.

https://www.teachexcel.com/excel-tutorial/2387/elegant-date-and-location-filtering-in-excel-a-must-see
tonyimbruglia (rep: 6) Dec 17, '22 at 10:07 pm
Glad that Don's video suited your needs, Tony - I agree it's a super tutorial. Thanks for selecting my answer. 
John_Ru (rep: 6152) Dec 18, '22 at 2:08 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login