Delete all but rows 1 - 10

imback2nite

Board Regular
Joined
Oct 30, 2004
Messages
203
Office Version
  1. 2003 or older
Platform
  1. Windows
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
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
WOW! The members here are amazing! I'm experimenting with all your suggestions. Thank you so much for the speediness of your responses!
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

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