简体   繁体   中英

Excel-VBA: Get Value of a Visible Cell in a Table after applying Filter?

I am trying to get the first visible cell in a table (also known as) ListObject in a simple way.

Code so far:

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

With your code, you should be initialising your variables.

Something like this should work (corrollary - this is untested)

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
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

Use it like so:

getFirstVisibleCellInTable("Data")

To only retrive value:

    getFirstVisibleCellInTable("Data").Value

To only retrive address:

getFirstVisibleCellInTable("Data").Address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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