Delete all but rows 1 - 10

imback2nite

Board Regular
Joined
Oct 30, 2004
Messages
113
Office Version
  1. 2007
Thank you for looking. Is there a way I can keep rows 1-10 from being deleted?

What I am doing is deleting rows selected by the user. All, that is, but for rows 1-10. These must not be deleted. The user will select the rows they want deleted then select my delete button. This will unprotect the page and delete the row and then protect it again. I am using this code. I appreciate all help!
VBA Code:
Private Sub CommandButton2_Click()

ActiveSheet.Unprotect

Dim answer As Integer 'Delete Lines

answer = MsgBox("Have You Selected The Row(s) To Delete?", vbQuestion + vbYesNo + vbDefaultButton2, "Delete Row")

If answer = vbYes Then

Selection.EntireRow.Delete

Else

If answer = vbNo Then

MsgBox "Please Select Row To Delete.", , "Delete Row"

End If

End If

Application.ScreenUpdating = False

ActiveSheet.Protect

End Sub
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

davesexcel

Well-known Member
Joined
Feb 26, 2006
Messages
1,089
VBA Code:
    Dim answer As Integer
    
    If Selection.Row < 11 Then
        MsgBox "selection must be below row 10"
        Exit Sub
    End If
    
    ActiveSheet.Unprotect
 
    answer = MsgBox("Have You Selected The Row(s) To Delete?", vbQuestion + vbYesNo + vbDefaultButton2, "Delete Row")
    
    If answer = vbYes Then
        Selection.EntireRow.Delete
    Else
        If answer = vbNo Then
            MsgBox "Please Select Row To Delete.", , "Delete Row"
        End If
    End If
    
    ActiveSheet.Protect
 

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
2,147
Another possible approach, in addition to what @davesexcel has already suggested:
VBA Code:
If Not Application.Intersect(Selection, Rows("11:" & Rows.Count)) Is Nothing Then
    Application.Intersect(Selection, Rows("11:" & Rows.Count)).Select
Else
    MsgBox ("Rows 1 to 10 cannot be deleted, make another selection")
    Exit Sub
End If
Insert this snippet just after the Dim and before ActiveSheet.Unprotect
It will remove rows 1:10 from the selection, o rise a msgbox and terminate if no any deleteable row has been selected

Also: be aware that Selection.Row will return the initial row of the first selected area, if two or more areas are selected. So if the user select B20:G50 and then adds B1:B5, Selection.Row will return 20

Bye
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,822
Office Version
  1. 365
Platform
  1. Windows
Another option
VBA Code:
Private Sub CommandButton2_Click()

ActiveSheet.Unprotect

Dim answer As Integer 'Delete Lines
Dim Rng As Range

answer = MsgBox("Have You Selected The Row(s) To Delete?", vbQuestion + vbYesNo + vbDefaultButton2, "Delete Row")

If answer = vbYes Then

Set Rng = Intersect(Selection.EntireRow, Rows("11:" & Rows.Count))
If Not Rng Is Nothing Then Rng.Delete

Else

If answer = vbNo Then

MsgBox "Please Select Row To Delete.", , "Delete Row"

End If

End If


ActiveSheet.Protect

End Sub
 

imback2nite

Board Regular
Joined
Oct 30, 2004
Messages
113
Office Version
  1. 2007
WOW! The members here are amazing! I'm experimenting with all your suggestions. Thank you so much for the speediness of your responses!
 

Watch MrExcel Video

Forum statistics

Threads
1,128,176
Messages
5,629,176
Members
416,370
Latest member
Lgathana

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
Top