Diable insert/delete of rows in workbook

SHenry

Board Regular
Joined
Mar 28, 2006
Messages
91
I have a spreadsheet in which I would like to disable any row deleting or inserting... I would use the protect sheet but I have data grouping that I would like to preserve.

Any ideas? Or will I have to live without data grouping?

thanks for any help,

Stu
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

If you protect the sheet you can't use the XL Grouping fonctionality and if you don't you can but you also allow the user inserting/deleting rows within your grouped data which is undesirable.

If it's ok for you to use Macros in your workbook then one solution to your problem consists on the following hack:

1- Enter a volatile Function in a hidden cell on your worksheet. You can choose the "Now()" Function for example.

2- Give your Data table a Range Name.

3- Place the code below in your Worksheet Module:

Code:
Option Explicit

Private iDataRangeRows As Integer
Private iDataRangeColumns As Integer
Private Const sMsg = " Caught you ! You can't insert/delete Rows in this table"

Private Sub Worksheet_Calculate()

    On Error Resume Next
    With Application
        If Range("MyDataRange").Rows.Count <> iDataRangeRows _
        And iDataRangeRows <> 0 Then
            .EnableEvents = False
            Application.Undo
            MsgBox sMsg, vbCritical
            .EnableEvents = True
        End If
        If Range("MyDataRange").Columns.Count <> iDataRangeColumns And _
        iDataRangeColumns <> 0 Then
            .EnableEvents = False
            Application.Undo
            MsgBox sMsg, vbCritical
            .EnableEvents = True
        End If
    End With

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    iDataRangeRows = Range("MyDataRange").Rows.Count
    iDataRangeColumns = Range("MyDataRange").Columns.Count

End Sub
Where "MyDataRange" is the range Name given to your table .

Now, the integrity of your grouped data structure will always be preserved and everytime the user attempts to insert or delete a Row or Column within the table, the code will impede it.

Here is a workbook demo: http://www.savefile.com/files/1163359

Regards.
 
Upvote 0
Jaafar

This is out of my field of expertise, but I was wondering if my logic works here? That is, rather than looking at rows and columns separately, couln't we just look at cells as follows?
Code:
Option Explicit

Private iDataRangeCells As Integer

Private Sub Worksheet_Calculate()

    On Error Resume Next
    With Application
        If Range("MyDataRange").Cells.Count <> iDataRangeCells Then
            .EnableEvents = False
            Application.Undo
            .EnableEvents = True
        End If
    End With

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    iDataRangeCells = Range("MyDataRange").Cells.Count

End Sub


I note that with either code, you can select "half a row" and insert/delete cells (shift cells down/up) and then select the other "half row" and do the same.
 
Upvote 0
Peter-
couldn't we just look at cells

Absolutly right. I got too distracted with the Row/Column thing that i forgot the simpler Cells count solution.

note that with either code, you can select "half a row" and insert/delete cells (shift cells down/up) and then select the other "half row" and do the same.

Good catch. However, strictly speaking, pushing a cell down/right or even deleting a cell doesn't affect the integrity of the table structure. It's like editing a cell within the table. Notice that the named range address as well as the Group Outline don't shift with the cells .

Having said that, it would be nice to see if inserting/shifting/deleting a cell in that manner could also be prevented.

Regards.
 
Upvote 0
Jaafar

Thanks for the explanation. And, yes, I had noticed
... pushing a cell down/right or even deleting a cell doesn't affect the integrity of the table structure. It's like editing a cell within the table. Notice that the named range address as well as the Group Outline don't shift with the cells .
 
Upvote 0
I just noticed a small problem - users could still insert a new row 1... what could be added to the code to prevent this?

PS: I'm using rafaaj's code

Stu
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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