简体   繁体   English

在字段中使用变量而不是数字进行自动过滤

[英]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:我的程序需要读取所有列并检测那些写有“Inspecc.tornillo”或“”caracterización exfoliación”的列。我的意思是,宏必须与不同的 excel 一起使用,而您不知道列的顺序,您只需知道其中之一将具有名称“caracterización exfoliación”等等。所以我尝试以这种方式使用 Autofilter:

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.我将变量 tornillo 声明为变体,但它不起作用。 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 :我们要找到Treasure列并过滤它的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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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