Prevent insert or delete rows and column with Excel VBA

Posted on 2015-01-12
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
  • 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 11

Assisted Solution

by:Tej Pratap Shukla ~Dexter
Tej Pratap Shukla ~Dexter earned 200 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

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

LVL 43

Accepted Solution

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

Author Closing Comment

ID: 40561280

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

770 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