Code to prevent inserting rows and columns

FGaxha

Board Regular
Joined
Jan 10, 2023
Messages
227
Office Version
  1. 365
Platform
  1. Windows
Pls how to prevent inserting rows and columns in excel. This code not working:

Sub DisableInsertRowColumn() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") ws.Protect Password:="YourPassword", AllowInsertingRows:=False, AllowInsertingColumns:=FalseEnd Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Copy and paste this code into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the code into the empty code window that opens up. Close the code window to return to your sheet. Try inserting a row or column.
VBA Code:
Dim lRow As Long, lCol As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol = ActiveSheet.UsedRange.Columns.Count
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Dim lRow2 As Long, lCol2 As Long
    lRow2 = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol2 = ActiveSheet.UsedRange.Columns.Count
    If lRow2 <> lRow Or lCol2 <> lCol Then
        Application.Undo
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
In the future, please use Code Tags when posting your VBA code.
It is extremely easy, only takes a second, and will make your code in a more user friendly format like mumps code is.
See: How to Post Your VBA Code

Your code is not very readable with all the wrapping like that. Luckily it is very short, otherwise it might be totally impossible to work with.
 
Upvote 0
What does not work?
If mumps code does not work, why did you mark & accept it as the solution?
 
Upvote 0
I tested it on a dummy sheet and it worked properly. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet.
Alternately, you could upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
 
Upvote 0
I marked it before testing
Please test it and confirm it is working before marking it as the solution!
Most people will not even bother looking at your question if it is marked as already having a solution.

Note that you can also remove the solution by checking on it again if it is not working.
I have done that for you here, but you can do it yourself in the future.
 
Upvote 1
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top