简体   繁体   中英

Autofilter using (Range as Criteria) works with strings , But not works with numbers (even after number converted to string)

Basically I want to use vba Autofilter using (Range as Criteria).
The below code works if the criteria range is explicitly strings (characters).
I have used excel interface to change the format of criteria range to be Text then I run my code,But the result of Autofilter is nothing.
I need to know what is wrong with my code and How to solve it.
In advance thanks for all your help.

Sub Filter_rng_criteria()
 
   Dim ws As Worksheet
   Set ws = ActiveSheet
 
   Dim rng As Range
   Set rng = ws.Range("A2:R4605")
 
   If Not ws.AutoFilterMode Then rng.AutoFilter                       'Set AutoFilter if not already set
 
   Dim rng_criteria As Range
   Set rng_criteria = ws.Range("T2", ws.Range("T2").End(xlDown))      'Rnage from T2 till Down
 
   Dim arr
   arr = Application.WorksheetFunction.Transpose(rng_criteria.Value)  'Range to Criteria
 
   rng.AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues
 
End Sub

It seems that criteria expects explicit string-values:-/

if you add

   Dim i As Long
   For i = LBound(arr) To UBound(arr)
        arr(i) = CStr(arr(i))
   Next

after the transpose -row it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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