Friday, October 4, 2013

Handy Functions for Tables (ListObject)

Functions to get row number or column number for an active cell selected in the table

Function GetActiveCellTableRowNum() As Long
    Dim currTable As ListObject
    Set currTable = ActiveCell.ListObject
    
    If currTable Is Nothing Then
        GetActiveCellTableRowNum = -1
    Else
        GetActiveCellTableRowNum = _ 
            ActiveCell.Row - currTable.HeaderRowRange.Row
    End If
End Function


Function GetActiveCellTableColNum() As Long
    Dim currTable As ListObject
    Set currTable = ActiveCell.ListObject
    
    If currTable Is Nothing Then
        GetActiveCellTableColNum = -1
    Else
        GetActiveCellTableColNum = _
           currTable.ListColumns(ActiveCell.Column - _
           currTable.HeaderRowRange.Cells(1).Column + 1).Index
    End If
End Function

No comments: