Home > database >  Autofilter using (Range as Criteria) works with strings , But not works with numbers (even after num
Autofilter using (Range as Criteria) works with strings , But not works with numbers (even after num

Time:09-20

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

CodePudding user response:

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

  • Related