Shyam Sundar Shankar
Shyam Sundar Shankar

Reputation: 96

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

Upvotes: 2

Views: 3160

Answers (2)

siggi_pop
siggi_pop

Reputation: 657

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

Upvotes: 3

Tragamor
Tragamor

Reputation: 3634

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

Upvotes: 3

Related Questions