简体   繁体   中英

Autofilter using variable Criteria VBA

I am trying to put an autofilter on a worksheet using a variable, but it is not working.

  Dim Criteria As Object
  i = 1
  Set Criteria = ActiveSheet.Cells(i, 1)
  MsgBox (Criteria.Value) ' this returns BC01.03

However, when I try to filter "Criteria.Value" is not returning anything.

Selection.AutoFilter
ActiveSheet.Range("$A$1:$BM$204").AutoFilter Field:=2, Criteria1:=" & Criteria.Value & "

Please advice.

Get rid of the quote marks around your Criteria1 argument:

ActiveSheet.Range("$A$1:$BM$204").AutoFilter Field:=2, Criteria1:=Criteria.Value

Also, just a question why you would use late binding on a Range object? I do use late binding for objects that would require another library/reference, but since Range is part of the Excel object library, I would use early binding. I would Dim Criteria as Range but that's just my preference.

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