自动过滤使用变量表示列号

问题描述

在以下代码中,我试图用Long替换命名列“ I”以自动过滤:

With Range("I1",Range("I" & Rows.Count).End(xlUp))
    .AutoFilter 1,TeamString
End With

我已将“ filterColumn” 定义为Long。但是我遇到了运行时错误:对象失败的方法范围,因为我在下面对变量的引用不正确:

With Range(Cells(filterColumn,1),Range(filterColumn & Rows.Count).End(xlUp))
    .AutoFilter 1,filterEntry
End With

解决方法

请尝试以下方法:

With Range(Cells(1,filterColumn),Cells(Rows.Count,filterColumn).End(xlUp))
    .AutoFilter 1,filterEntry
End With

Cells参数是RowIndex,后跟ColumnIndex