How to fix date filter VBA as it is not picking up all dates that fall within my range

﹥>﹥吖頭↗ 提交于 2021-01-20 12:06:47

问题


I'm trying to create a filter that will filter out all dates that fall within the dates I choose. The dates I choose will always reflect a whole month.

For example, if I need data for May 2019, I will input my start date as 01/05/2019 and my end date as 31/05/2019.

My data filter will need to pick up all rows that go through my start date. So it should pick up whole calendar years (01/01/2019 - 31/12/2019), quarters (01/04/2019 - 30/06/2019) and any dates that cover the month of May.

Currently my filter only picks up whole calendar years and some (but not all)exact dates such as 01/05/2019 - 31/05/2019. So I'm missing a lot of information such as the dates that fall as quarters, or over a number of months surrounding May.

I've attempted to play around with the way that it filters by moving around the less than and more than sign but this just meant that a lot of the deals became excluded.

I use this filter on another workbook with much less data and it picks up all data. Whereas on this workbook, there is much more data and the start dates vary a lot, so I lose about half the data I need.

Sub Filter()
    Dim lngStart As Long, lngEnd As Long
    lngStart = Range("AI1").Value 'this is the start date
    lngEnd = Range("AI2").Value 'this is the end date

    Range("G2:G5000").AutoFilter field:=7, _ 'this is the start date column
        Criteria1:="<=" & lngEnd, _
        Operator:=xlAnd, _
        Criteria2:="<=" & lngEnd

    Range("H2:H5000").AutoFilter field:=8, _ 'this is the end date column
        Criteria1:=">=" & lngStart, _
        Operator:=xlAnd, _
        Criteria2:=">=" & lngStart


End Sub

I would like all my data that passes through May 2019 (example month) to show when I click on my filter. It is only showing one of the month deals, and one of the quarter deals when there are five more.


回答1:


Dates are tricky in VBA filters. For one thing, the VBA Date data type is not quite the same as an Excel value formatted as a date.

And I think your logic was a bit off also.

This seems to work on your sample data.

Sub Filter()
    Dim lngStart As Long, lngEnd As Long, fltrRng As Range
    lngStart = Range("J1").Value 'this is the start date
    lngEnd = Range("J2").Value 'this is the end date
    Set fltrRng = Range("A2:H5000")


    With fltrRng
        .AutoFilter field:=7, _
        Criteria1:=">=" & CDbl(lngStart)

        .AutoFilter field:=8, _
       Criteria1:="<" & CDbl(lngEnd)
    End With

End Sub

Note that we defined the entire filter range, but I would define it differently, and dynamically, so as not to encompass more rows than necessary.

With Worksheets("sheet1")
    Set fltrRng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 8).End(xlUp))
End With

Also, we converted the lngStart and lngEnd to the Double data type, which is how dates are stored in Excel on the worksheet. You could have just declared them as being of type Double. So, with all that:

Sub Filter()
    Dim lngStart As Double, lngEnd As Double, fltrRng As Range
    lngStart = Range("J1").Value 'this is the start date
    lngEnd = Range("J2").Value 'this is the end date

    With Worksheets("sheet1")
        Set fltrRng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 8).End(xlUp))
    End With

    With fltrRng
        .AutoFilter field:=7, _
        Criteria1:=">=" & lngStart

        .AutoFilter field:=8, _
       Criteria1:="<" & lngEnd
    End With

End Sub

Finally, the logic should state, I believe, that you want dates where

  • the start column is equal or greater than the start date AND
  • the end column is less than the end date (which, you have entered as one date past the actual desired end date, which is proper.

EDIT:

Another problem is that some of your dates are not "real Excel dates". In other words, they are textual representations of dates in a format that is different from your regional windows settings, where the Month entry is > 12.

This problem usually arises when a text or csv file is OPEN'd, rather than IMPORT'd, and the date format in the file is different from the date format in the Windows Regional Settings for the computer.

By IMPORTing a csv file, one can specify the date format of the incoming data and avoid this problem.

To demonstrate it in the file you provided, change the filtering part of the macro to also pick up text dates. eg:

With fltrRng
    .AutoFilter field:=7, _
    Criteria1:=">=" & lngStart, _
        Operator:=xlOr, _
    Criteria2:=Format(lngStart, """*""/mm/yyyy")

    .AutoFilter field:=8, _
   Criteria1:="<" & lngEnd, _
        Operator:=xlOr, _
    Criteria2:=Format(lngEnd - 1, """*""/mm/yyyy")

End With

However, this is far from ideal because, most likely, even the dates that are rendered as "real Excel dates" were probably translated incorrectly. In other words, if the CSV file contained 05/01/2019 meaning 5-Jan-2019, it would be translated to 1-May-2019 in the scenario cited.



来源:https://stackoverflow.com/questions/56127468/how-to-fix-date-filter-vba-as-it-is-not-picking-up-all-dates-that-fall-within-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!