Excel vba选择过滤后的第一个可见行

时间:2015-07-08 08:32:46

标签: excel vba filter

我正在尝试过滤特定值“COUNTRY1”的列表,然后选择标题下方的前10行(从第2行A列到第11行L列)或(“A2:L11”)

然而,由于过滤器,第一个可见行不是(“A2”),而是例如(“A15”)。

我想要做的是指向第一个可见行,复制前10行,但只复制到L列而不是整行。
搜索了一些在线参考文献,但它们非常复杂,并且有更多细节没有帮助。

任何帮助都会得到真正的赞赏。

以下代码:

wbk5.Sheets("Consolidated").Activate

With wbk5.Sheets("Consolidated")
    .AutoFilterMode = False
    .Range("A1:D1").AutoFilter
    .Range("A1:D1").AutoFilter Field:=1, Criteria1:="COUNTRY1"
    .Range("A2:L11").Copy
End With

wbk5.Sheets("TOP 10-C").Activate
wbk5.Sheets("TOP 10-C").Range("A6").PasteSpecial Paste:=xlPasteValues

1 个答案:

答案 0 :(得分:0)

With wbk5.Sheets("Consolidated")
    .AutoFilterMode = False
    .Range("A1:D1").AutoFilter
    .Range("A1:D1").AutoFilter Field:=1, Criteria1:="COUNTRY1"
    Dim Cols As Range, Rows1 As Range, Counter As Long, RowNum As Long
    RowNum = 10 'quota of ten
    'edit that might help
    Counter = 0
    '/edit
    Set Cols = .Columns("A:L") 'columns from where we need data
    For i = 2 To .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious) 'from the second row to the last
        If .Cells(i, 1).Visible And Counter < RowNum Then 'if it's visible and we haven't filled our quota
            Counter = Counter + 1 'we're closer to our quota
            Set Rows1 = .Range("2:" & i) ' we can include this row
        End If
    Next i
    Intersect(Cols, Rows1).SpecialCells(xlCellTypeVisible).Copy 'from selected columns, from selected rows, only visible cells
End With