2014-10-07 89 views
0

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

回答

1

这里有一个想法,尝试获取(活动行 - 表的第一行)。这会给你表中的行号。

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 
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 
1
Selection.Row - Selection.ListObject.Range.Row 
相关问题