从所选单元格中查找表格行号

时间:2014-10-07 08:57:03

标签: excel-vba row vba excel

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

5 个答案:

答案 0 :(得分:3)

    Selection.Row - Selection.ListObject.Range.Row

答案 1 :(得分:1)

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

答案 2 :(得分:0)

这可能会有所帮助,假设工作表中只有一个表。否则您需要指定表格范围。

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

答案 3 :(得分:0)

我不是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

答案 4 :(得分:0)

YourRange.row 减去 YourListObject.HeaderRowRange.row