在自动过滤后,从F列的第一个可见行中拉出值

时间:2014-05-10 17:45:44

标签: excel vba

如何在自动过滤器后从F列的第一个可见行中提取值?我想Debug.Print VisibleCount()中的值。

Sub GetPrimaryContacts()

Dim Col As New Collection
Dim itm
Dim i As Long
Dim CellVell As Variant

'Get last row value
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

'Loop between all rows to get unique values
For i = 3 To LastRow
    CellVal = Sheets("Master").Range("F" & i).Value
    On Error Resume Next
    Col.Add CellVal, Chr(34) & CellVal & Chr(34)
    On Error GoTo 0
Next i

' Create workbooks - Token Not activated
Call TokenNotActivated
For Each itm In Col
    ' If IsNull(itm) Then Debug.Print "None" Else Debug.Print itm
    ActiveSheet.Range("A2:Z2").Select
    Selection.AutoFilter Field:=6, Criteria1:=itm
    Call VisibleCount
Next

ActiveSheet.AutoFilter.ShowAllData

End Sub

Sub VisibleCount()
    Dim r As Range, n As Long, itm
    n = Cells(Rows.Count, 1).End(xlUp).Row
    Set r = Range("A1:A" & n).Cells.SpecialCells(xlCellTypeVisible)
    If r.Count - 2 > 0 Then Debug.Print itm & "-" & r.Count - 2
End Sub

1 个答案:

答案 0 :(得分:1)

此功能类似于我在上一个问题中发布的内容。它将第一个可见行作为Range返回。我留给你选择该范围内的细胞:

Function GetFirstFilterAreaRow(ws As Excel.Worksheet) As Excel.Range
Dim FilterArea As Excel.Range
Dim FirstRow As Excel.Range

With ws.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
    If .Areas(1).Rows.Count >= 2 Then
        Set FirstRow = .Areas(1).Rows(2)
    'if the first row is filtered out
    ElseIf .Areas.Count >= 2 Then
        Set FirstRow = .Areas(2).Rows(1)
    Else
        'No visible filtered values
        'This Else clause is only for clarity, not actually required
        Set FirstRow = Nothing
    End If
End With
Set GetFirstFilterAreaRow = FirstRow
End Function 

阅读AutoFilter.Range property

可能会有所帮助