自动筛选两个不同行中的数据块。

huangapple go评论48阅读模式
英文:

AutoFiltering two separate block of data in different rows

问题

第一部分的代码可以保持不变,但是第二部分的代码需要将字段字段(Field)更改为2,以匹配列A中的数据。修改后的代码如下所示:

With ActiveSheet.Range("$A$508:$D$537")
    .AutoFilter Field:=2, Criteria1:="<>"
End With
英文:

In a worksheet I'm working on a the moment there are two sections that need to be filtered to show when data is in the cells.

The first section has data in column B and is working fine using the code

With ActiveSheet.Range(&quot;$A$48:$D$507&quot;)
    .AutoFilter Field:=2, Criteria1:=&quot;&lt;&gt;&quot;
End With

The next handful of rows have data sitting in column A, but applying the same code as above doesn't seem to include the rows that have data in them. The code I've used is

With ActiveSheet.Range(&quot;$A$508:$D$537&quot;)
    .AutoFilter Field:=1, Criteria1:=&quot;&lt;&gt;&quot;
End With

What needs to be changed or added to get the second block of data showing?

答案1

得分: 0

另一位用户评论说,在一个工作表上不能同时使用多个范围的自动筛选(尝试手动操作而不使用VBA - 如果尝试应用第二个筛选条件,它会删除第一个)。

但是,您可以在一个工作表上使用多个表格的自动筛选。首先将您的范围转换为表格,然后像下面这样筛选表格:

ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=2, Criteria1:="<>"
ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=1, Criteria1:="<>"
英文:

As another user commented, you cannot have autofilters on multiple ranges on one sheet (try doing it manually without VBA - if you try to apply a 2nd filter it will remove the 1st one).

However you can have autofilters on multiple tables on one sheet. Convert your ranges to tables first, then filter the tables like below:

ActiveSheet.ListObjects(&quot;Table1&quot;).Range.AutoFilter Field:=2, Criteria1:=&quot;&lt;&gt;&quot;
ActiveSheet.ListObjects(&quot;Table2&quot;).Range.AutoFilter Field:=1, Criteria1:=&quot;&lt;&gt;&quot;

huangapple
  • 本文由 发表于 2023年6月2日 07:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定