[ad_1]
I am trying to filter between a date range in excel using vba.
It is not working.
However when I perform a manual filter, it works.
So I recorded the macro and applied the same code.
Again, it did not work.
Date format in file is as below:
Let’s say I am trying to filter out 01 to 31 July 2021.
Here’s my code that does not work: Date in in A column.
startDT = ActiveSheet.txtStartDT.Text '01-Jul-2021
endDT = ActiveSheet.txtEndDT.Text '31-Jul-2021
For Each srcWsht In srcWbk.Worksheets
lastSrcRow = srcWsht.Range("A1").End(xlDown).Row
srcWsht.Range("A2:A" & lastSrcRow).Replace What:="'", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
srcWsht.Range("A2:A" & lastSrcRow).NumberFormat = "dd-MM-yyyy"
srcWsht.Range("A1:O" & lastSrcRow).AutoFilter Field:=1, Criteria1:=">=" & CDate(startDT), Operator:=xlAnd, Criteria2:="<=" & CDate(endDT), visibledropdown:=True
srcWsht.Range("A2:O" & lastSrcRow).SpecialCells(xlCellTypeVisible).Copy
wshtData.Range("A" & lastDataRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
lastDataRow = wshtData.Range("A2").End(xlDown).Row + 1
Next srcWsht
[ad_2]