Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from deleting a particular sheet

Tags:

excel

vba

Protecting workbook structure will prevent a user from deleting sheets. But how could I (using VBA) prevent a user from deleting a particular sheet I designate? I've seen examples where an active sheet is prevented from deletion by

Set mymenubar = CommandBars.ActiveMenuBar
mymenubar.Controls("Edit").Controls("Delete sheet").Visible = False

in its Worksheet_Activate event but that of course only works if the sheet is activated.
Is there a way to prevent a sheet from being deleted whether active or no?
For clarity: I'm fine with the user deleting some sheets, just not a couple of particular sheets.
So protecting workbook structure won't work.

like image 489
user3474688 Avatar asked May 14 '14 04:05

user3474688


People also ask

How do I prevent people from deleting a sheet in Excel?

To keep all worksheets in an Excel file from being erased, just activate the Protect Workbook function in Excel's Review tab. This feature can prevent changes to an Excel workbook's structure and windows.

How do I exclude a specific worksheet when printing a workbook?

Using an easy Macro: Just click on the first tab you want to print and make it active. Then hold down control, and click on each additional tab you want to print, excluding the tab, or tabs you don't want to print. Then go to "file" and then "print" like you normally would.

How do I enable the Delete Sheet option in Excel?

Delete Sheets Using Right-Click OptionsRight-click on the sheet that you want to delete. Click on the delete option. In the prompt that shows up, click on the Delete button.


1 Answers

As far as I can tell, it isn't possible to natively tag a single sheet as non-deletable; and there isn't an event that can be used to detect when a sheet is about to be deleted so the workbook can be protected preventively.

However, here is one potential workaround:

  1. Protect workbook structure: this will, as you indicate, prevent all sheets from being deleted.
  2. Create a "Controls" sheet. On this sheet, maintain a list of all sheet names (except those you don't want to be deletable).
  3. If users want to delete a sheet, they will have to select its name on the Controls sheet (e.g. in a data validation drop-down menu) and press a "Delete" button. This button will call a macro that temporarily unprotects the workbook, deletes the selected sheet, and then reprotects the workbook.

Of course, the users will have to get used to this way of deleting sheets (as opposed to just right-click > Delete on the sheet's tab). Still, this isn't crazy complicated.

As for how to achieve #2 i.e. maintaining that list of sheet names, I suppose you could make use of a UDF like this one (must be called as an array formula):

Function DeletableSheetNames() As String()
    Application.Volatile
    Dim i As Long
    Dim sn() As String
    With ThisWorkbook
        ReDim sn(1 To .Sheets.Count)
        For i = 1 To .Sheets.Count
            With .Sheets(i)
                If .Name = "DataEntry1" Or .Name = "DataEntry2" Then
                    'Don't include it in the list.
                Else
                    sn(i) = .Name
                End If
            End With
        Next i
    End With
    DeletableSheetNames = sn
End Function
like image 177
Jean-François Corbett Avatar answered Sep 28 '22 07:09

Jean-François Corbett