Solved

Prevent insert or delete rows and column with Excel VBA

Posted on 2015-01-12
6
687 Views
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 ?

Tks
0
Comment
Question by:AXISHK
  • 3
  • 2
6 Comments
 
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?
0
 
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) 

     'ProtectSheet
    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 
            .Undo 
            MsgBox "No deleting rows or columns", 16 
            .EnableEvents = True 
        End With 
         
    Else 
         
        Exit Sub 
         
    End If

Open in new window

0
 
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
0
Technology Partners: 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

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

Tks
0
 
LVL 43

Accepted Solution

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

Author Closing Comment

by:AXISHK
ID: 40561280
Tks
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

733 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