简体   繁体   English

运行时错误:Range AutoFilter上的'1004'

[英]Run-time error: '1004' on Range AutoFilter

What I want to do with VBA - filter table with array and delete rows. 我想用VBA做什么 - 带有数组和删除行的过滤表。 My array has 4 elements (changed in a loop to have 5 different sets). 我的数组有4个元素(在循环中更改为5个不同的集合)。 Column which is being filtered has 5 elements. 正在过滤的列有5个元素。 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. 我想得到的只是1.这是一个循环,它将创建5个报告,每个报告根据第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. 如果在调试模式下,我手动过滤该列并跳过该行,我在第二次尝试再次清除所有过滤器时出现相同的错误5行。

This are lines 765 and 770 out of part of 788. Everything else runs smoothly. 这是788部分的765和770行。其他一切运行顺利。

FilterOutArray - array with 4 elements (Variant/String each). FilterOutArray - 包含4个元素的数组(每个都是Variant / String)。 Column 29 has 5 different values, no blanks 第29列有5个不同的值,没有空格

I've tried to refer "Criteria1" with string, to see if filtering one elements works, but not. 我试图用字符串引用“Criteria1”,看看过滤一个元素是否有效,但不是。

'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 . “有趣”的部分是这样的: Cells(1,1),Cells(2,2).Address是一个有效的语法,给出相应单元格的地址,右列1列,下行1行,例如B2

Anyway, in general, consider How to avoid using Select in Excel VBA . 无论如何,一般来说,请考虑如何避免在Excel VBA中使用Select And the "correct" way to write ranges defined by 2 cells is something like this: 写2个单元格定义范围的“正确”方法是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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