简体   繁体   中英

Run-time error: '1004' on Range AutoFilter

What I want to do with VBA - filter table with array and delete rows. My array has 4 elements (changed in a loop to have 5 different sets). Column which is being filtered has 5 elements. I want to get just 1. This is inside a loop which will create 5 reports, each filtered for different element based on column 29.

If in debugging mode I filter that column manually and skip that line I have second, identical error 5 lines below when trying to clear all filters back again.

This are lines 765 and 770 out of part of 788. Everything else runs smoothly.

FilterOutArray - array with 4 elements (Variant/String each). Column 29 has 5 different values, no blanks

I've tried to refer "Criteria1" with string, to see if filtering one elements works, but not.

'Run-time error: '1004' on row below
ActiveSheet.Range(Cells(1, 1).Cells(29, LastRow)).AutoFilter Field:=29, Criteria1:=FilterOutArray, Operator:=xlFilterValues

Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp

'Run-time error: '1004' on row below
ActiveSheet.Range(Cells(1, 1).Cells(29, 9999)).AutoFilter Field:=29

In the line:

ActiveSheet.Range(Cells(1, 1).Cells(29, LastRow)) ...

Should be a comma, and not a dot. Like this:

ActiveSheet.Range(Cells(1, 1), Cells(29, LastRow)) ...

The "interesting" part is that something like this: Cells(1,1),Cells(2,2).Address is a valid syntax, giving the address of the corresponding cell, 1 column right and 1 row down, eg B2 .

Anyway, in general, consider How to avoid using Select in Excel VBA . And the "correct" way to write ranges defined by 2 cells is something like this:

With Worksheets(1)
    Set myRange = .Range(.Cells(1, 1), .Cells(5, 5))
End With

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