vba 从选定单元格中查找表格行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26232250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:00:16  来源:igfitidea点击:

find table row number from selected cell

excel-vbarowvbaexcel

提问by gvw

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.Rowor Selection.Row. But I want to know what row number in the table this is.

如何从所选单元格中找到表 (Excel 2010) 中的行号。
我可以从ActiveRow.Row或 中找到工作表行号Selection.Row。但我想知道这是表中的行号。

回答by Jalal

    Selection.Row - Selection.ListObject.Range.Row

回答by stolen_leaves

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

这是一个想法,尝试获取(活动行 - 表的第一行)。这将为您提供表格中的行号。

回答by tom_p_zh

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:

我不是 vba / excel 专家,但这可能会起作用:
答案有点晚了 - 但我遇到了同样的问题。
我的函数返回一个更强大的 listRow 对象:

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

回答by Anand

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