Prevent insert or delete rows and column with Excel VBA

Posted on 2015-01-12
Medium Priority
Last Modified: 2015-01-20
Is it possible to prevent a user from insert / delete a row or column on a particular worksheet with Excel VBA ?

Question by:AXISHK
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40543841
You can use worksheet protection for this. Why do you want to do this with VBA?
LVL 12

Assisted Solution

by:Tej Pratap Shukla ~Dexter
Tej Pratap Shukla ~Dexter earned 800 total points
ID: 40543843
Hi Axishk..

you should be able to accomplish this by protecting your sheet with the right parameters,

Insert the code in your Object Module to disable inserting Cells.

Private Sub Workbook_SheetActivate(ByVal Sh As Object) 

    ActiveSheet.Protect AllowInsertingColumns:=False, AllowInsertingRows:=False, UserInterfaceOnly:=True         
End Sub 

Open in new window

Code to prevent Deletion of rows and coloums

If ((Target.Address = Target.EntireRow.Address Or _ 
    Target.Address = Target.EntireColumn.Address)) Then 
        With Application 
            .EnableEvents = False 
            MsgBox "No deleting rows or columns", 16 
            .EnableEvents = True 
        End With 
        Exit Sub 
    End If

Open in new window

LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40543844
The VBA for the same is

    ActiveSheet.Protect DrawingObjects:=AllowInsertingColumns:=True, AllowInsertingRows:=True, _
        AllowDeletingColumns:=True, AllowDeletingRows:=True
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 40543896
How to modify the coding for checking the "INPUT" WORKSHEET only ?

LVL 43

Accepted Solution

Saqib Husain, Syed earned 1200 total points
ID: 40543938
Worksheets("Input").Protect AllowInsertingColumns:=True, AllowInsertingRows:=True, _
        AllowDeletingColumns:=True, AllowDeletingRows:=True

Author Closing Comment

ID: 40561280

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question