我有一个列表对象,我按日期过滤。一旦我有一个过滤的数组。我想在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
在D1
中有一个标题。当我过滤我的范围应该是D75:D90
...但msgbox会返回D2:D90
,就像数据未被过滤一样。
THX
答案 0 :(得分:1)
你快到了。只需使用 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
此外,您需要调整范围以明确处理具有值的单元格 这是为了补偿您的偏移以排除标题。
答案 1 :(得分:0)
检查必须循环区域的过滤项目,例如:
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