Search code examples
excelvbarowselection

How to detect user selecting an entire row and distinguish if multiple rows are selected?


How do I detect if the user selected an entire row on the worksheet?

I would like to return which row is selected so I can automate routines - such as copying the data associated with that row.

I want the automation to be applicable to any one row and not multiple rows.


Solution

  • Put this in the code of the worksheet you want it to run on. You can also check the 'address' property to determine where the selection is. This isn't a full answer, but it should get you started.

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim bEntireRow As Boolean
    With Target   
        bEntireRow = .Address = .EntireRow.Address
    End With
        If bEntireRow = True Then
            MsgBox (Target.Rows.Count & " Rows Selected")
        End If
    End Sub