Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does vba care about autofilter?

Tags:

excel

vba

If I apply auto-filter on my input sheet and then run VBA code, the code does not care about the auto-filter.

But, sometimes running VBA code on an auto-filtered sheet messes up the results of the program.

So, my question is; does VBA care about auto-filter?

For example:

Sub check()
    Dim rng as range
    Set rng = Sheets("input").Range("A1")
    row = 0
    Do until rng.offset(row,0) = ""
        row = row + 1
    Loop
End Sub

In the above code, VBA does not care if auto-filter is applied on column A, and it still iterates through all the rows. However, if I try to write on cells where there is auto-filter, it messes up.

like image 368
Kartik Anand Avatar asked Oct 20 '25 18:10

Kartik Anand


1 Answers

VBA Doesn't care about Autofilter unless you "tell it" to or are trying to perform actions which can get affected by the Autofilter.

Your above code will work with any sheet and not just with "Input" Sheet.

Here is another method where it works beautifully (in fact I use it all the time)

'~~> Remove any filters
ActiveSheet.AutoFilterMode = False

'~~> Filter, offset(to exclude headers) and delete visible rows   
With rRange 
  .AutoFilter Field:=1, Criteria1:=strCriteria
  .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

'~~> Remove any filters
ActiveSheet.AutoFilterMode = False

And here is a scenario when it doesn't work.

Charts don't show data which were filtered by Autofilter. But then the chart also doesn't show data which is present in hidden rows. This applies to both VBA and Non VBA methods of showing data in the chart.

but if i try to write on particular cells where autofiler is applied it messes up.

It depends on how and where you are writing it.

This works very nicely. Note in the below code, row has been filtered and is not visible. However, we can still write to it.

Option Explicit

Sub Sample()
    Dim rng As Range

    Set rng = Sheets("Sheet1").Range("A1")

    rng.AutoFilter Field:=1, Criteria1:="<>1", Operator:=xlAnd

    rng.Offset(1, 0).Value = "Sidd"
End Sub

enter image description here

Now let's take another case. This will not work. Let's say you have a range A2 to A10 (A1 has Header) which has various values ranging from 1 to 3. Now you want to replace all the values in A2:A10 by say 1000. This code will not give you the expected output if there is an Autofilter. It will not change all the cells.

Option Explicit

Sub Sample()
    Dim rng As Range

    Set rng = Sheets("Sheet1").Range("A1:A10")

    rng.AutoFilter Field:=1, Criteria1:="<>1", Operator:=xlAnd

    rng.Value = "1000"
End Sub

Why does it ignore the cells which have "1" (i.e the rows which were filtered out) and writes to rest of the rows? In fact it messes up with the header as well???

enter image description here

It's quite simple. The idea of having Autofilter is get the relevant data as per what our requirement is (at the moment it is data which is <> 1). When you write to the range rng then it will write to all cells which are visible (including the cell which has header) in that range.

So what do we do in this case?

You have two options

1) Remove the Autofilter - Do the necessary actions - Put the filter back

Sub Sample()
    Dim rng As Range

    Set rng = Sheets("Sheet1").Range("A1:A10")

    '~~> Put Filter
    rng.AutoFilter Field:=1, Criteria1:="<>1", Operator:=xlAnd

    '~~> Remove Filter
    ActiveSheet.AutoFilterMode = False

    '~~> Write value to the cells (See how we ignore the header)
    Sheets("Sheet1").Range("A2:A10").Value = "1000"

    '~~> Put Filter back
    rng.AutoFilter Field:=1, Criteria1:="<>1", Operator:=xlAnd
End Sub

2) Loop the range as you did in your question

Sub Sample()
    Dim rng As Range, cl As Range

    Set rng = Sheets("Sheet2").Range("A1:A10")

    rng.AutoFilter Field:=1, Criteria1:="<>1", Operator:=xlAnd

    For Each cl In rng
        '~~> Ignoring the Header
        If cl.Row <> 1 then _
        cl.Value = "1000"
    Next
End Sub

When you run the above code, it writes to all the cells except the header.

I would recommend you to read Excel's inbuilt help to understand how AutoFilters actually work. That would help you understand them which will in turn help you handle sheets which have Autofilter turned on.

HTH

like image 95
Siddharth Rout Avatar answered Oct 22 '25 13:10

Siddharth Rout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!