VBA过滤器停留在第二行

时间:2018-04-20 20:29:10

标签: excel vba excel-vba filter

我使用以下代码在多张纸上进行过滤:

方案是我正在从一个工作簿复制数据并将其粘贴到当前工作簿。表格(表1至表13 S)从表格中提取数据,并通过公式复制数据。我使用相同的代码用于具有相同结构化数据的其他工作簿,唯一的区别是我复制了不同的行。

由于某些奇怪的原因,在本工作簿中,从表1到表13,文件管理器卡在第二行。但是,对于表1 S至表13 S,滤波器都是正常的。

我为了这个哈哈拉我的头发......

Set y = ActiveWorkbook

y.Sheets("Table 1").Range("AA2").AutoFilter Field:=27, Criteria1:="x"
y.Sheets("Table 2").Range("AB2").AutoFilter Field:=28, Criteria1:="x"
y.Sheets("Table 3").Range("AC2").AutoFilter Field:=29, Criteria1:="x"
y.Sheets("Table 4").Range("AD2").AutoFilter Field:=30, Criteria1:="x"
y.Sheets("Table 8").Range("AE2").AutoFilter Field:=31, Criteria1:="x"
y.Sheets("Table 11").Range("AF2").AutoFilter Field:=32, Criteria1:="x"
y.Sheets("Table 12").Range("AG2").AutoFilter Field:=33, Criteria1:="x"
y.Sheets("Table 13").Range("AH2").AutoFilter Field:=34, Criteria1:="x"

y.Sheets("Table 1 S").Range("AA2").AutoFilter Field:=27, Criteria1:="x"
y.Sheets("Table 2 S").Range("AB2").AutoFilter Field:=28, Criteria1:="x"
y.Sheets("Table 3 S").Range("AC2").AutoFilter Field:=29, Criteria1:="x"
y.Sheets("Table 4 S").Range("AD2").AutoFilter Field:=30, Criteria1:="x"
y.Sheets("Table 8 S").Range("AE2").AutoFilter Field:=31, Criteria1:="x"
y.Sheets("Table 11 S").Range("AF2").AutoFilter Field:=32, Criteria1:="x"
y.Sheets("Table 12 S").Range("AG2").AutoFilter Field:=33, Criteria1:="x"
y.Sheets("Table 13 S").Range("AH2").AutoFilter Field:=34, Criteria1:="x"

1 个答案:

答案 0 :(得分:2)

I wasn't able to replicate the exact problem you are having. There is probably something different between the worksheets that makes Excel guess the filtered range incorrectly. I don't know what the root cause of the problem is, but I think telling excel the explicit range to put the filter on will fix the problem. Otherwise excel is guessing where you want the range.

For example, if the range of the data (including headers) for Table 1 is AA1 to AF500 then I would make the code look like this:

y.Sheets("Table 1").Range("AA1:AF500").AutoFilter Field:=27, Criteria1:="x"

If the range is dynamic VBA has a lot of different ways to handle that. So you can explicitly specify a dynamic range using one of those techniques.