find table row number from selected cell
Asked Answered
F

6

6

How do I find the row number in a table (Excel 2010) from the selected cell.
I can find the sheet row number from ActiveRow.Row or Selection.Row. But I want to know what row number in the table this is.

Flivver answered 7/10, 2014 at 8:57 Comment(0)
O
16
    Selection.Row - Selection.ListObject.Range.Row
Ostium answered 29/10, 2017 at 11:56 Comment(1)
Awesome. Thanks! ➕💯Blate
N
1

Here's an idea, try getting (active row - first row of table). That will give you the row number from the table.

Ngo answered 7/10, 2014 at 9:3 Comment(0)
F
1

i am not a vba / excel expert but this might do the job:
the answer is a bit late - but i ran into the same problem.
my function returns a listRow object, that is more powerful:

Sub testit()
    Dim myList As ListObject
    Dim myRow As ListRow
    'some reference to a listObject
    Set myList = ActiveWorkbook.Sheets(1).ListObjects("TableX")
    '
    'test the function
    Set myRow = FirstSelectedListRow(myList)
    '
    'select the row
    myRow.Select
    'get index within sheet
    MsgBox ("sheet row num " & myRow.Range.Row)

   ' get index within list
   MsgBox ("List row index " & myRow.Index)
End Sub
'return ListRow if at least one cell of one row is acitve
'return Nothing otherwise
Function FirstSelectedListRow(list As ListObject) As ListRow
    'default return
    Set FirstSelectedListRow = Nothing
    'declarations
    Dim activeRange As Range
    Dim activeListCells As Range
    Dim indexSelectedRow_Sheet As Long
    Dim indexFirstRowList_Sheet As Long
    Dim indexSelectedRow_List As Long
    'get current selection
    Set activeRange = Selection
    Set activeListCells = Intersect(list.Range, activeRange)
    'no intersection - test
    If activeListCells Is Nothing Then
        Exit Function
    End If
    indexSelectedRow_Sheet = activeRange.Row
    indexFirstRowList_Sheet = list.Range.Row
    indexSelectedRow_List = indexSelectedRow_Sheet - indexFirstRowList_Sheet
    Set FirstSelectedListRow = list.ListRows(indexSelectedRow_List)
End Function
Flooring answered 20/1, 2015 at 17:12 Comment(0)
E
0

This might help, assuming that there is only one table in sheet. Otherwise You need to specify the table range.

Sub FindRowNoInTable()

Dim ObjSheet As Worksheet
Dim startRow, ActiveRow, ActiveCol
Dim ObjList As ListObject
Set ObjSheet = ActiveSheet
ActiveRow = ActiveCell.Row
ActiveCol = ActiveCell.Column
For Each ObjList In ObjSheet.ListObjects
    Application.Goto ObjList.Range
    startRow = ObjList.Range.Row
Next
MsgBox (ActiveRow - startRow)
Cells(ActiveRow, ActiveCol).Select

End Sub
Experienced answered 8/10, 2014 at 15:26 Comment(0)
A
0

YourRange.row minus YourListObject.HeaderRowRange.row

Albuminoid answered 25/2, 2021 at 1:53 Comment(0)
V
0

A little late, but I landed here whilst searching for this, so ...

Simplest way I know:

    dim MyTable as listobject
    dim MyRow as integer

    ' Here your code or a manual action makes you end up on a random row
    ... Dostuffdostuffbleepbloop ...

    ' Find the row number you landed on within the table (the record number if you will)
    MyRow = MyTable.DataBodyRange().Row

That's it.

Pease note:

  • This is done with Office 2019
  • The brackets after DataBodyRange are EMPTY.
  • The row counter is from DataBodyRange thus does NOT include the headers

Hope it helps someone.

Vish answered 2/1, 2023 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.