Sub test()
Dim tbl As Object
Dim i As Long
Set tbl = ActiveSheet.ListObjects("tblCosting")
For i = 1 To tbl.ListRows.Count
If Selection.Address = tbl.ListRows(i).Range.Address Then
MsgBox "Row: " & i & " is selected."
Exit Sub
End If
Next
MsgBox "No table rows selected"
End Sub
range.rows(i).Address
Sub MM1()
If Intersect(ActiveCell, ActiveSheet.ListObjects("tblCosting").DataBodyRange) Is Nothing Then
MsgBox "activecell is NOT within the table"
Else
MsgBox "active cell is within the table"
End If
End Sub
igold's code doesn't comprehend the possibility that multiple, perhaps non-contiguous, table rows are selected - perhaps that's the source of your unexpected results.Whether I have a row selected or not, I get the message box saying No table rows selected.
Sub test()
Dim tbl As Object, Ar As Range
Dim i As Long, j As Long, k As Long
Set tbl = ActiveSheet.ListObjects("tblCosting")
If TypeName(Selection) = "Range" Then
If Not Intersect(Selection, tbl.Range) Is Nothing Then
For i = 1 To Selection.Areas.Count
For j = 1 To Selection.Areas(i).Rows.Count
For k = 1 To tbl.ListRows.Count
If Selection.Areas(i).Rows(j).Address = tbl.ListRows(k).Range.Address Then
MsgBox "Row: " & k & " of the table is selected."
End If
Next k
Next j
Next i
Else
MsgBox "No table rows selected"
End If
Else
MsgBox "Current selection is not a range object"
End If
End Sub
Maybe something like this......I guess the obvious question is WHY do you need to do this ???
Code:Sub MM1() If Intersect(ActiveCell, ActiveSheet.ListObjects("tblCosting").DataBodyRange) Is Nothing Then MsgBox "activecell is NOT within the table" Else MsgBox "active cell is within the table" End If End Sub
igold's code doesn't comprehend the possibility that multiple, perhaps non-contiguous, table rows are selected - perhaps that's the source of your unexpected results.
This code will catch multiple row selections within the table including non-contiguous:
Code:Sub test() Dim tbl As Object, Ar As Range Dim i As Long, j As Long, k As Long Set tbl = ActiveSheet.ListObjects("tblCosting") If TypeName(Selection) = "Range" Then If Not Intersect(Selection, tbl.Range) Is Nothing Then For i = 1 To Selection.Areas.Count For j = 1 To Selection.Areas(i).Rows.Count For k = 1 To tbl.ListRows.Count If Selection.Areas(i).Rows(j).Address = tbl.ListRows(k).Range.Address Then MsgBox "Row: " & k & " of the table is selected." End If Next k Next j Next i Else MsgBox "No table rows selected" End If Else MsgBox "Current selection is not a range object" End If End Sub
Sub MM1()
ActiveCell.EntireRow.Select
If Intersect(ActiveCell.EntireRow, ActiveSheet.ListObjects("tblCosting").DataBodyRange) Is Nothing Then
MsgBox "active row is NOT within the table"
Else
MsgBox "active row is within the table"
End If
End Sub