Determine if ActiveCell is in a specific table

I am trying to define VBA in Excel 2013 if ActiveCell is not only in any table, but also in a specific table.

The code below is as is, but only detects that ActiveCell is in any table. The commented line is what I'm looking for, but obviously this does not work.

    ...
    Set rng = Intersect (.EntireRow, ActiveCell.ListObject.DataBodyRange)
    'Set rng = Intersect (.EntireRow, ActiveCell.ListObjects ("myTable"). DataBodyRange)
    On Error GoTo 0
        If rng Is Nothing Then
            MsgBox "Please select the cell of a row within the consensus input table.", VbCritical, "Delete Evaluator"
        Else
    ...

Any suggestions on the correct syntax for this?

Thanks!

+4
source share
3 answers

To check if ActiveCell is in the body of table 1:

Sub qwerty()

   If Intersect(ActiveCell, ActiveSheet.ListObjects("Table1").DataBodyRange) Is Nothing Then
      MsgBox "activecell not in Table1"
   Else
      MsgBox "activecell in Table1"
   End If

End Sub
+5
source

More general solution, adaptable to other tables

Sub Demo()
    Dim r As Range
    Dim lo As ListObject

    Set r = ActiveCell
    Set lo = r.ListObject
    If Not lo Is Nothing Then
        Select Case lo.Name
            Case "Table1"
                If r.Row = lo.Range.Row Then
                    MsgBox "In Table1 Header"
                Else
                    MsgBox "In Table1 Body"
                End If
            Case "SomeOtherTable"
                '...
        End Select
    Else
        MsgBox "Not in any table"
    End If
End Sub
+2
source

, , DataBodyRange , Excel . "myTable" DataBodyRange [myTable].

, ActiveCell :

If Not Intersect(ActiveCell, [myTable]) Is Nothing Then
0

Source: https://habr.com/ru/post/1618493/


All Articles