select一个已筛选的范围
我有一个列表对象,我按date过滤。 一旦我有一个过滤的数组。 我想在Column D
上设置一个范围,使其具有一个读取过滤范围的For循环
这是我的代码
ActiveSheet.ListObjects("Invoices").Range.AutoFilter Field:=5, Criteria1:="=" & Year.value ActiveSheet.ListObjects("Invoices").Range.AutoFilter Field:=6, Criteria1:="=" & Month.value Dim r As Range Set r = Sheets("Invoices").Range(Range("D1"), Range("D1").End(xlDown)).Offset(1, 0) MsgBox r.Address
Column D
有一个标题。 当我过滤我的范围应该是D75:D90
…但msgbox返回D2:D90
,如果数据没有被过滤。 谢谢
你快到了。 只需使用如下的SpecialCells属性 :
Dim r As Range, c As Range With Sheets("Invoices").Range(Range("D1"), Range("D1").End(xlDown)) Set r = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible) End With '~~> Use Areas property if you want to loop '~~> through the filtered data as posted by lukas2 For Each c In r.Areas Msgbox c.Address Next
你也需要调整你的范围的大小,以显式的使用值的单元格。
这是为了补偿你的偏移量,以排除标题。
检查过滤的项目,你必须循环的地区,例如:
Dim Filtered As Range Dim a As Variant Dim g As Variant Set Filtered = Selection.SpecialCells(xlCellTypeVisible) For Each a In Filtered.Areas For Each g In a.Rows MsgBox g.Address Next g Next a