如何仅访问VBA范围内的已过滤行?

时间:2011-03-23 14:26:41

标签: excel-vba vba excel

我希望能够在VBA中使用AutoFilter方法的结果。我可以应用AutoFilter方法(使用命名范围),但无法弄清楚如何在VBA中创建仅包含结果的Range变量。然后我想循环遍历这个结果范围。我不知道如何在不检查每一行的Visible属性并对这些行进行操作的情况下如何做到这一点。

Public Sub CopyFilteredRows()
   Dim sourceRg As Range, filteredRg As Range, objRow As Range

   Set sourceRg = ThisWorkbook.Names("FY10CountsRg").RefersToRange
   sourceRg.AutoFilter Field:=1, Criteria1:="=D-144", Operator:=xlOr, _
     Criteria2:="=D-200"

   For Each objRow In filteredRg.Rows
      ''do something
   Next

End Sub

1 个答案:

答案 0 :(得分:3)

试试这个 - 它应该只是击中可见细胞和打印他们的价值观 - 你应该能够调整它的工作:

Dim rgAreas As Range: Set rgAreas = FilteredRg.SpecialCells(xlCellTypeVisible)
Dim rgArea  As Range
Dim rgCell  As Range

For Each rgArea In rgAreas.Areas
    For Each rgCell In rgArea.Cells
        Debug.Print rgCell.Address & ": " & rgCell.Value
    Next rgCell
Next rgArea