VBA code to see if an entire row has been selected.

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need some code to see if an entire row has been selected within a range and also I need code to see if an entire row has been selected within a table, tblCosting.

Thanks,
Dave
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Does this work for your table.

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


You could do the same thing for a range. Instead of tbl.Listrows(i), you could use

Code:
range.rows(i).Address
 
Upvote 0
What would be the code to see if a row has not been selected?
 
Upvote 0
Sorry, I didn't read the rest of the code. I will try it now.
 
Upvote 0
Whether I have a row selected or not, I get the message box saying No table rows selected.
 
Upvote 0
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
 
Upvote 0
Whether I have a row selected or not, I get the message box saying No table rows selected.
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
 
Upvote 0
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

How do I change the code to fire the alert if an entire row in the table is not selected?
 
Upvote 0
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

This code seems to work, thanks. Now for some more testing and I might have to get back to you.
 
Upvote 0
Maybe this....but my question remains.....WHY ???

Code:
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
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top