Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA code to prevent certain cell range from being edited but can still delete rows when needed

I have a shared excel sheet that is essentially a table where it should not go beyond column X. I found this VBA code to make it so that anything from range Y1:XFD1048576 cannot have any information or data added to it, but now I cannot delete any rows in the table when needed. How do I fix this?

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range(“Y1:XFD1048576”),Target) Is Nothing Then
        Application.EnableEvents = False
        Application.Undo
        Application.EnableEvents = True
    End If
End Sub

I tried finding ways to edit the code but to no avail. Haven’t really been able to find anything for this specific problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This might do what you need:

The modified code now checks if the the cells being changed are entire rows (i.e. you’re deleting or adding a row) and if it is, does not perform the undo.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("Y:XFD"), Target) Is Nothing _
      And Not Target.EntireRow.Address = Target.Address Then
        Application.EnableEvents = False
        Application.Undo
        Application.EnableEvents = True
    End If
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading