简体   繁体   中英

Excel VBA autofilter between percentages

I want to get the total number of rows where the density percentage is more than 60% but less than 90%.

I am doing this through VBA to add filters on density column (percentage format):

With ActiveSheet
        .AutoFilterMode = False
                With .Range("A3:EB3")

                     .AutoFilter
                     .AutoFilter Field:=98, Criteria1:=">9"
                     .AutoFilter Field:=99, Criteria1:=">2014"
                     .AutoFilter Field:=23, Criteria1:=">60%"
                     .AutoFilter Field:=23, Criteria1:="<90%"

                End With
    End With

But auto filter displays no rows

To apply the BETWEEN criteria to column W in a Range.AutoFilter Method , you need to bring both the lower and upper boundaries into the same command line.

    Dim cnt As Long

    With Worksheets("Sheet2")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A3:EB" & .Cells(Rows.Count, "W").End(xlUp).Row)

            .AutoFilter
            .AutoFilter Field:=98, Criteria1:=">9"
            .AutoFilter Field:=99, Criteria1:=">2014"
            .AutoFilter Field:=23, Criteria1:=">60%", _
                        Operator:=xlAnd, Criteria2:="<90%"
            cnt = Application.Aggregate(2, 7, .Columns(23))
            Debug.Print cnt
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

Your second filter command for field 23 was cancelling the first. However, you should still check the validity of your data. It seems to me that if the lower boundary of the percentage filter was cancelled, your would get more entries than you wanted, not none at all.

autofilter_between_pcts_before
Sample data before 'between percentages' filter is applied

autofilter_between_pcts_after Sample data after 'between percentages' filter is applied

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM