I need to delete selected rows in a template.
Selection.EntireRow.Delete handles all my scenarios, but one.
When I select the same row, in multiple selections Selection.EntireRow.Delete will throw a 1004 Error.
I've written an ErrorHandler to warn about the situation, and then resume to not leave the template unlocked.
Sub DelRows()
    Dim answer As VbMsgBoxResult, LRow As Integer
    LRow = Range("D5").Value
    
    If Selection.Cells(1, 1).Row < 9 Or Selection.Cells(1, 1).Row + Selection.Rows.Count > LRow - 1 Then                                            
        MsgBox "Forbidden selection."
    ElseIf Selection.Rows.Count > LRow - 11 Then                                                                                                    
        MsgBox "Can't delete all rows."
    Else
        On Error GoTo ErrHandler
        ActiveSheet.Unprotect Password:="xx"
        answer = MsgBox("Are you sure you want to delete all rows?", vbOKCancel + vbExclamation, "Warning")
        If answer = 1 Then Selection.EntireRow.Delete                        
        Rows((LRow + 2) & ":1048576").EntireRow.Hidden = True                               
        ActiveSheet.Protect Password:="xx", DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, AllowFormattingRows:=True, AllowFiltering:=True
    
    End If
    Exit Sub
    ErrHandler:
        If Err.Number = 1004 Then
            MsgBox "Can't select the same row twice: " & Err.Description
            Resume Next
        Else
            MsgBox Err.Description
            Resume Next
    End If
End Sub
The thing works, but my experience with error handling isn't the best.
Can I avoid this error with some other  method?
Can I check the selection for multiple selections in the same row, or isn't it worth the effort of doing so?
I could probably loop all selected cells, create an array containing the rows for each area and then compare the results, but that seems excessive.
--Final solution--
Thanks to Pᴇʜ I managed to not only solve the problem, but find and solve two more at the same time.
Multiple selections break my Selection.Cells(1, 1).Row and Selection.Rows.Count as they only care about the first area. Which can both be fixed by looping the areas.
    Dim answer As VbMsgBoxResult, LRow As Long, i As Long, RowCount As Long, TopRow As Long
    LRow = Range("D5").Value
    TopRow = Selection.Cells(1, 1).Row
    
    For i = 1 To Selection.Areas.Count
        RowCount = RowCount + Selection.Areas(i).Rows.Count
        If Selection.Areas(i).Cells(1, 1).Row < TopRow Then TopRow = Selection.Areas(i).Cells(1, 1).Row
    Next i
    
    If TopRow < 9 Or TopRow + RowCount > LRow - 1 Then                                         
        MsgBox "Forbidden selection."
    ElseIf RowCount > LRow - 11 Then                                                                                                    
        MsgBox "Can't delete all rows."
    Else
        ActiveSheet.Unprotect Password:="xx"
        answer = MsgBox("Are you sure you want to delete all rows?", vbOKCancel + vbExclamation, "Warning")
        If answer = 1 Then Selection.EntireRow.Delete                        
        Rows((LRow + 2) & ":1048576").EntireRow.Hidden = True                               
        ActiveSheet.Protect Password:="xx", DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, AllowFormattingRows:=True, AllowFiltering:=True
    
    End If
End sub
 
     
     
    