简体   繁体   中英

Autofilter using in Field a variable instead of a number

I am doing a new macro where I use autofilters. My program needs to read all columns and detect those where is written "Inspecc. tornillo" or ""caracterización exfoliación". I mean, the macro has to work with different excels where you don't know the order of the columns, you just know that one of them will have the name "caracterización exfoliación" and so on. So I tried to use Autofilter in this way:

ActiveSheet.Range(Cells(1, 1), Cells(nFilas, nColumnas)).AutoFilter   
**Field:=tornillo**, Criteria1:="=Inspeccionar", Operator:=xlOr, Criteria2:="=No"

Instead of:

> ActiveSheet.Range(Cells(1, 1), Cells(nFilas, nColumnas)).AutoFilter   
> **Field:=7**, Criteria1:="=Inspeccionar", Operator:=xlOr, Criteria2:="=No"

I declared the variable tornillo as variant, but it doesn't work. How could I do?

Thanks in advance

Say we have:

在此处输入图片说明

and we do not know which column is the Treasure column. We want to find the Treasure column and filter it for gold :

Sub TreasureHunt()
    Dim N As Long

    N = Range("1:1").Find(what:="Treasure", after:=Range("A1")).Column
    ActiveSheet.Cells.AutoFilter Field:=N, Criteria1:="Gold"
End Sub

在此处输入图片说明

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