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

后端 未结 1 1892
面向向阳花
面向向阳花 2021-01-28 09:31

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

相关标签:
1条回答
  • 2021-01-28 10:20

    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.

    0 讨论(0)
提交回复
热议问题