【问题标题】:find table row number from selected cell从选定的单元格中查找表格行号
【发布时间】:2014-12-01 15:48:03
【问题描述】:

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

【问题讨论】:

    标签: excel row vba


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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】:

        我不是 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】:
              Selection.Row - Selection.ListObject.Range.Row
          

          【讨论】:

          • 太棒了。谢谢! ➕?
          【解决方案5】:

          YourRange.row 减去 YourListObject.HeaderRowRange.row

          【讨论】:

            猜你喜欢
            • 2012-05-16
            • 1970-01-01
            • 2014-09-04
            • 1970-01-01
            • 2013-04-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多