Solved

Prevent insert or delete rows and column with Excel VBA

Posted on 2015-01-12
6
431 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
Comment Utility
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
Comment Utility
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
Comment Utility
The VBA for the same is

    ActiveSheet.Protect DrawingObjects:=AllowInsertingColumns:=True, AllowInsertingRows:=True, _
        AllowDeletingColumns:=True, AllowDeletingRows:=True
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:AXISHK
Comment Utility
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
Comment Utility
Worksheets("Input").Protect AllowInsertingColumns:=True, AllowInsertingRows:=True, _
        AllowDeletingColumns:=True, AllowDeletingRows:=True
0
 

Author Closing Comment

by:AXISHK
Comment Utility
Tks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now