Ensure Selection Row Deletion Doesn't Extend Past Certain Row

  • I have a delete button on a protected sheet to allow a user to delete selected rows (unprotects, deletes, protects again...).


    I need to add a check to make sure they do not delete any row greater than 152. How can I check if a row greater than 152 is in their range of selected rows?


    Thanks

  • Re: Check If A Certain Row Number Is In A Selection


    Here's a start:


    Sub DeleteRows()


    Code
    If ActiveCell.Row > 152 Then
        MsgBox "Row deletion not permitted"
        
    Else
        ActiveCell.EntireRow.Delete
        
    End If
    
    
    End Sub
  • Re: Check If A Certain Row Number Is In A Selection


    If the user has more than one row selected then try


    Code
    If Selection.Cells(Selection.Cells.Count).Row >= 152 Then
        MsgBox "Cannot delete beyond row 152!!"
    Else
        Selection.EntireRow.Delete
    End If


    VBA Noob

  • Re: Check If A Certain Row Number Is In A Selection


    If a user deletes a row below 153, row 153 becomes row 152. Is the user now allowed to delete row 152


    Edited

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Check If A Certain Row Number Is In A Selection


    Yeah, the way the sheet was set up (not by me), there is a totals row at 153 and some other info after that which the user should not be able to delete. So I ran into the problem that bill mentioned, where 153 became 152, which doesn't work because I need that row to always be at 153.

    I ended up clearing the contents (except the fomulas), cutting the rows and moving the to the bottom of the range. It's not too elegant, but it works:




    Thanks for the replies

  • Re: Check If A Certain Row Number Is In A Selection


    Another approach would be to make the "may not be deleted" range a named range. Then

    Code
    If Not(Application.Intersect (Range("DoNotDeleteMe"),Selection) is Nothing) Then
        Selection.Delete
    End If

    would protect that range, while Excel keeps track of where insertions and deletions have moved DoNotDeleteMe.

  • If you wish to continue using this free service.


    Please be considerate of others who use the forum for searching. Your current Thread Title (which I will change) is non-reflective of your immediate problem.


    In future, please take 1 minute of your time to read the text on the New Thread page.


    [fa]*[/fa]


    REMEMBER: Your thread title should NEVER be what you THINK is your answer. 9 times out 10 it will wrong and prevent someone from finding a solution to a simliar issue.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!