简体   繁体   English

Excel vba 过滤表 BodyRange 不正确 SpecialCells

[英]Excel vba Filtered table BodyRange incorrect SpecialCells

I have a table which I filter with a vba command:我有一个表,我使用 vba 命令过滤它:

tbl.Range.AutoFilter Field:=11, Criteria1:="=*" & Left(street, 3) & "*", Operator:=xlAnd

It's a *contains* filter, BodyRange (check visually) should count 7 rows, but with this这是一个 *contains* 过滤器,BodyRange(目视检查)应该计算 7 行,但是有了这个

tbl.DataBodyRange.SpecialCells(xlCellTypeVisible)

I only retrieve less just 4 of them, it's behaving like the BodyRange.SpecialCells capture from the filter only the values beginning with the filter value but not the ones containing it (if my filtered column street are "pilsudzki" "al. pilsudzki" "al. gen pilsudzki" BodyRange will skip all the "al. pilsudzki" etc... and give me only the "pilsudzki" ie the one beginning with " pils *")我只检索不到其中的 4 个,它的行为就像 BodyRange.SpecialCells 从过滤器中捕获仅以过滤器值开头的值而不是包含它的值(如果我过滤的列街道是“pilsudzki”“al.pilsudzki”“ al. gen pilsudzki" BodyRange 将跳过所有的 "al. pilsudzki" 等...并且只给我 "pilsudzki" 即以 " pils *" 开头的那个)

to get around this I must use this trick instead:为了解决这个问题,我必须改用这个技巧:

Set rnTble = Sheet1.Range("K2:K150000").SpecialCells(xlCellTypeVisible)

For Each cll In rnTble.Cells
Me.ListBox2.AddItem cll.Value
If cll.Value = "" Then Exit For
Next

then and only then I capture all the filtered value containing "*pils*"然后只有这样我才能捕获所有包含“*pils*”的过滤值

Any ideas why is it that way?任何想法为什么会这样?

The filtered part is the data divided into several areas.过滤部分是将数据分成几个区域。 Because they are separated, you need to use areas.因为它们是分开的,所以您需要使用区域。

For Each rng In rngTble.Areas
    For Each cll In rng
        Me.ListBox2.AddItem cll.Value
    Next cll
Next rng

No filter没有过滤器

在此处输入图像描述

filtered state过滤 state

在此处输入图像描述

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

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