简体   繁体   English

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

[英]How do I access just the filtered rows in a range from VBA?

I want to be able to use the results of an AutoFilter method within VBA. 我希望能够在VBA中使用AutoFilter方法的结果。 I can apply the AutoFilter method (using a named range), but can't figure out how to create a Range variable in VBA that only includes the results. 我可以应用AutoFilter方法(使用命名范围),但是无法弄清楚如何在VBA中创建仅包含结果的Range变量。 I then want to loop through this result range. 然后,我想遍历此结果范围。 I can't figure out how to do this without simply checking every row for its Visible property and acting on those rows. 我不知道如何简单地检查每一行的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

Try this - it should just hit the visible cells & print their values - you should be able to tweak it do the job: 尝试此操作-它应该只击可见单元格并打印其值-您应该可以对其进行调整:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM