过滤后如何获取表中可见单元格的值?

时间:2018-11-03 18:19:50

标签: excel vba autofilter listobject

我试图以一种简单的方式获取表ListObject中的第一个可见单元格。

到目前为止的代码:

Sub StatusFilter()

Set WB = ThisWorkbook
Set iFace = WB.Sheets("Interface")
Set DataS = WB.Sheets("Data")

iCriteria = iFace.Range("Q22").Value
DataS.Activate
ActiveSheet.ListObjects("Data").Range.AutoFilter 14, iCriteria

ActiveSheet.ListObjects("Data").DataBodyRange.Select

With Columns("A")
    .Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
DValue = ActiveCell.Value

If DValue = "" Then
    MsgBox "Lucky! No Tickets are in this Criteria!!", vbInformation, "Technology Issue Tracker"
    Exit Sub
End If

End Sub

2 个答案:

答案 0 :(得分:3)

使用代码,您应该初始化变量。

类似的事情应该起作用(必然的-未经测试)

Sub StatusFilter()

    Dim WB As Workbook: Set WB = ThisWorkbook
    Dim iFace As Worksheet: Set iFace = WB.Sheets("Interface")
    Dim DataS As Worksheet: Set DataS = WB.Sheets("Data")

    Dim iCriteria As String: iCriteria = iFace.Range("Q22")
    Dim DValue As String

    With DataS.ListObjects("Data").Range
        .AutoFilter 14, iCriteria
        DValue = Index(.SpecialCells(xlCellTypeVisible), 1).Value
    End With

    If DValue = "" Then
        MsgBox "Lucky! No Tickets are in this Criteria!!", vbInformation, "Technology Issue Tracker"
        Exit Sub
    End If

End Sub

答案 1 :(得分:3)

Function getFirstVisibleCellInTable(tblName As String) As Range

Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects(tblName)

For i = 1 To tbl.ListRows.Count
    If False = tbl.ListRows(i).Range.EntireRow.Hidden Then
        Set getFirstVisibleCellInTable = tbl.DataBodyRange(i, 1)
        Exit Function
    End If
Next i

getFirstVisibleCellInTable = Nothing

End Function

像这样使用它:

getFirstVisibleCellInTable("Data")

仅获取值:

    getFirstVisibleCellInTable("Data").Value

要仅检索地址:

getFirstVisibleCellInTable("Data").Address