Prevent user from deleting a particular sheet

前端 未结 6 1292
春和景丽
春和景丽 2020-12-10 06:54

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\'

6条回答
  •  庸人自扰
    2020-12-10 07:09

    I can prevent a sheet from being deleted via the Worksheet_BeforeDelete Event as follows:

    Private Sub Worksheet_BeforeDelete()
    
        Call ThisWorkbook.Protect("password")
    
        Call MsgBox("This sheet cannot be deleted.", vbExclamation)
    
    End Sub
    

    This protects all sheets from being deleted, however if you add some event code on the ThisWorkbook module like the following :

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    
        Call ThisWorkbook.Unprotect("password")
    
    End Sub
    

    I will then be able to delete any other sheet as soon as it is selected.

    Bear in mind, you will lose copy and paste functionality between pages due to the page unlocking when it is selected.

提交回复
热议问题