prevent row deletion

annadinesh

Board Regular
Joined
Mar 1, 2017
Messages
105
Dear Experts

Please provide a code for Disable/Restrict Row Delete for top 4 Rows only, after row 5 we can delete the rows,


Regards


Dinesh Saha
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello!
Paste this code into sheet you want to prevent deletion:
VBA Code:
Public RowCnt As Long
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range(Rows(1), Rows(4))) Is Nothing And RowCnt <> ActiveSheet.UsedRange.Rows.Count Then
    With Application
    .EnableEvents = False
    .Undo
    .EnableEvents = True
    End With
  Else
    Exit Sub
  End If
End Sub

Paste this code into Workbook:
VBA Code:
Private Sub Workbook_Open()
  Worksheets("Sheet1").RowCnt = ActiveSheet.UsedRange.Rows.Count 'change Sheet1 with your actual sheet name.
End Sub
 
Upvote 0
Hi Flashbond,

AFAIK Public Variables must be defined in standard modules - trying to use your code RowCnt shows no value.

Holger
 
Upvote 0
Yes I tought the same. Also RowCnt in open workbook event is empty. But RowCnt shows a value if you break at this line. Weird..
VBA Code:
  If Not Intersect(Target, Range(Rows(1), Rows(4))) Is Nothing And RowCnt <> ActiveSheet.UsedRange.Rows.Count Then
 
Upvote 0
@HaHoBe I added that check for not to alarm on column deletes. Do you have another trick to bypass column deletions?
 
Upvote 0
Hi Flashbond,

I would add a hidden name for a range once and refer to this name inside Worksheet_Change.

Adding the Name may look like this (change sheetname and range name/area to suit but make sure to refer to the proper Name in Worksheet_Change):

VBA Code:
Sub SetHiddenName()

  Dim oName           As Name
 
  Const cstrRgName    As String = "KeepRows"
  Const cstrRgRange   As String = "TestForKeepHeaderRows!A1:ZZ4"

  For Each oName In ActiveWorkbook.Names
    If oName.Name = cstrRgName Then
      oName.Delete
      Exit For
    End If
  Next oName

  ActiveWorkbook.Names.Add Name:=cstrRgName, _
                           RefersTo:="=" & cstrRgRange, _
                           Visible:=False

End Sub

Code behind the sheet (in this sample TestForKeepHeaderRows):

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("KeepRows")) Is Nothing Then
    With Application
    .EnableEvents = False
    .Undo
    .EnableEvents = True
    End With
  Else
    Exit Sub
  End If
End Sub

You may run SetHiddenName again if you want to refer to a different area (more or less rows for the header) after changing the constant cstrRgRange.

Ciao,
Holger
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,170
Members
449,296
Latest member
tinneytwin

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