Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

Excel vba Filtered table BodyRange incorrect SpecialCells

I have a table which I filter with a vba command:

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

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" i.e the one beginning with "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*"

Any ideas why is it that way?

question from:https://stackoverflow.com/questions/66054423/excel-vba-filtered-table-bodyrange-incorrect-specialcells

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

filtered state

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...