Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if ActiveCell is in a specific table

Tags:

excel

vba

I am trying to determine by VBA in Excel 2013 if the ActiveCell is not just in any table, but in a specific table.

Below is the code as is, but only detects ActiveCell being in any table. The commented out line is what I'm looking for, but obviously it doesn't 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 right syntax for this?

Thanks!

like image 757
K.C. Grimes Avatar asked Dec 03 '15 21:12

K.C. Grimes


People also ask

How do I select the active cell row?

Select one or more rows and columns Or click on any cell in the column and then press Ctrl + Space. Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space.

How do you select a cell relative to the active cell?

You can use the "offset" property to change which cell you want to select based on where your active cell is. This will offset the active cell down 0 rows and to the right 2 columns. If you ever want to go the other way just put (-)negative signs in front of the numbers. Hope this helps!

Where can you check the current active cell of a selected range?

Excel allows you to easily see the location of the currently selected cell by examining the contents of the Name Box, to the left of the Formula Bar.


1 Answers

To test if ActiveCell is in the body of Table1:

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
like image 164
Gary's Student Avatar answered Oct 05 '22 19:10

Gary's Student