要使用自动过滤器的可见单元格,Offset
如果您打算排除标题,则必须使用。你得到的错误是因为你错过了一个“。” 前Cells(2,11)
'~~> Remove any filters
ActiveSheet.AutoFilterMode = False
'~~> Filter,
With rRange
.AutoFilter Field:=1, Criteria1:=strCriteria
'~~> offset(to exclude headers)
Debug.Print .Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells(2,11).Value
Debug.Print .SpecialCells(xlCellTypeVisible).Cells(2,11).Value
End With
'~~> Remove any filters
ActiveSheet.AutoFilterMode = False
我决定将此添加为此答案的一部分,以便将来可能对其他人有所帮助。
假设我们的范围是
A1:F6
当你运行下面的代码时,根据你是否使用Offset
,你会得到这些结果。
Option Explicit
Sub Sample()
'~~> Remove any filters
ActiveSheet.AutoFilterMode = False
Dim rRange As Range
Dim Rnge As Range
Set rRange = Sheets("Sheet1").Range("A1:F6")
'~~> Filter,
With rRange
.AutoFilter Field:=1, Criteria1:="<>2"
'~~> Offset(to exclude headers)
Set Rnge = .Offset(1, 0).SpecialCells(xlCellTypeVisible)
Debug.Print Range(Rnge.Address).Address
Debug.Print ActiveSheet.Cells(3, 2).Address
Debug.Print Range(Rnge.Address).Cells(3, 2).Address
Debug.Print "--------------------------------------------------"
'~~> To include headers
Set Rnge = .SpecialCells(xlCellTypeVisible)
Debug.Print Range(Rnge.Address).Address
Debug.Print ActiveSheet.Cells(3, 2).Address
Debug.Print Range(Rnge.Address).Cells(3, 2).Address
End With
'~~> Remove any filters
ActiveSheet.AutoFilterMode = False
End Sub
高温高压